当前位置:网站首页>c# 单元测试
c# 单元测试
2022-07-26 02:07:00 【L-960】
1 前言
在开发asp.net core web api服务时,想要测试某个服务经常要为service编写一个测试的控制器,然后再通过api来访问进行测试,这样需要频繁的重启服务,而且调试起来也不方便,使用单元测试能够很好的解决这一问题。
2 测试框架选择
XUnit
其实测试框架有多种选择:
NUnit 、 XUnit 、 MSTest
NUnit 是 XUnit 的前生
XUnit 是面向 .NET Core 的.
MSTest 是微软自带的, 但是很少人用
微软官网三个教程都有,因为我们要对asp.net core进行测试,所以选则 XUnit
在csproj中添加依赖,或者在nuget中下载。
<Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>
</Project>
moq
moq 在 TDD 扮演很重要的角色
参考链接: moq4 Quickstart
Service 通常会有许多依赖, 我们在做单元测试时不能像spring一样直接使用这些依赖, 不然依赖有问题就会被误以为是这个 Service 的问题.
因为asp.net 觉得,我们测试的是这个方法,不需要依赖其他的类,所以当我们依赖于其他的类时,可以使用mock来构建所需的对象。
在csproj中添加依赖,或者在nuget中下载。
<Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<PackageReference Include="Moq" Version="4.18.1" />
</ItemGroup>
</Project>
3 使用示例
public class Test
{
private readonly ITestOutputHelper _testOutputHelper;
public Test(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
/// <summary>
/// 单元测试基础用法
/// </summary>
[Fact]
public void testBasic()
{
var mockConfiguration = new Mock<IConfiguration>();
mockConfiguration.SetupGet(obj => obj["AppSettings:TransUrl"]).Returns("127.0.0.1");
IConfiguration configuration = mockConfiguration.Object;
_testOutputHelper.WriteLine(configuration["AppSettings:TransUrl"]);
}
}
运行结果:
边栏推荐
- How to choose cloud note tool? What can I do with cloud notes?
- Redis cluster construction (based on 6.x)
- 3. Upload the avatar to qiniu cloud and display it
- What is JSX?
- Mark and lightbulbs (thinking)
- D - Dire Wolf (interval DP)
- Leetcode algorithm 147. insert and sort the linked list
- ggplot2学习总结
- # Dest0g3 520迎新赛(更新中)
- I.MX6UL核心模块使用连载-查看系统信息 (二)
猜你喜欢

Activiti workflow gateway
![[2019] [paper notes] tunable THz broadband absorption based on metamaterials——](/img/ae/bbebba30d0d6f58d65531cff48b5e9.png)
[2019] [paper notes] tunable THz broadband absorption based on metamaterials——

还在用==0 null equal 判断空值吗,对isEmpty 和 isBlank有多少了解呢

These practical security browser plug-ins improve your efficiency

Video game quiz? I think it's useless. It's better to do these well!

# Dest0g3 520迎新赛(更新中)

I.MX6UL核心模块使用连载-eMMC读写测试 (四)

【2021】【论文笔记】6G技术愿景——OTFS调制技术
![[2021] [paper notes] 6G technology vision - otfs modulation technology](/img/50/577ad05bc16e80d1c68eec7b6da988.png)
[2021] [paper notes] 6G technology vision - otfs modulation technology

I.MX6UL核心模块使用连载-Iot-6ULX核心模块简要介绍 (一)
随机推荐
LeetCode302场周赛第三题--裁剪数字后查询第 K 小的数字
JS add random pixel noise background to the page
【2021】【论文笔记】红外及THz下的细胞膜生物效应——效应是现象,作用是机理——THz对医学的好处
i. Mx6ull snvs power domain GPIO status hold verification
What is the difference between for... In... And for... Of
MPLS知识点
[xxl-job] xxl-job learning
The third question of leetcode 302 weekly Games -- query the number with the smallest k after cutting the number
Acwing 379. hide and seek problem solution (minimum path non repeating point coverage)
国标GB28181协议视频平台EasyGBS消息弹框模式优化
Sqlyog data import and export graphic tutorial
增删改查业务的快速上手
G. Count the trains (thought set + two points)
转:高效做正确的事
MySQL locking table problem
18_请求文件
National standard gb28181 protocol video platform easygbs message pop-up mode optimization
还在用==0 null equal 判断空值吗,对isEmpty 和 isBlank有多少了解呢
Phoenix中常用shell操作
19_请求表单与文件