当前位置:网站首页>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"]);
}
}
运行结果:
边栏推荐
- 1205 Lock wait timeout exceeded; try restarting transaction处理
- 由一个数据增量处理问题看到技术人员的意识差距
- [Android development IOS series] Language: swift vs kotlin
- [C language brush leetcode] 146. LRU cache (m)
- 登堂入室soc之编程基础环境变量设置
- 阿里云Redis开发规范
- [2021] [paper notes] biological effects of cell membrane under infrared and THz - effect is a phenomenon, action is a mechanism - the benefits of THz to medicine
- DQN Pytorch示例
- 商业智能BI全解析,探寻BI本质与发展趋势
- I.MX6UL核心模块使用连载-RTC测试 (十二)
猜你喜欢

What are the functions of cloud notes, and how do browsers add cloud note plug-ins

Activiti workflow gateway

I.MX6UL核心模块使用连载-以太网测试 (七)

# Dest0g3 520迎新赛(更新中)

Navica工具把远程MySQL导入到本地MySQL数据库

Implementation of Ti am335x industrial control module network and file system nfs

Bitmap这个“内存刺客”你要小心~

由一个数据增量处理问题看到技术人员的意识差距

【2019】【论文笔记】基于超材料可调谐THz宽频吸收——
![[independent station construction] Shopify seller: learn these points and double the sales volume of online stores!](/img/52/8c1520db38ffa8927e975b6f244a65.png)
[independent station construction] Shopify seller: learn these points and double the sales volume of online stores!
随机推荐
How to choose cloud note tool? What can I do with cloud notes?
Video game quiz? I think it's useless. It's better to do these well!
JS add random pixel noise background to the page
Master-slave replication in MySQL
[leetcode] 32. Longest valid bracket
由一个数据增量处理问题看到技术人员的意识差距
国标GB28181协议视频平台EasyGBS消息弹框模式优化
Ggplot2 learning summary
The import and Export button of Damon database table is gray, and the DMP file cannot be imported
Monitoring of debezium synchronization debezium
Navica工具把远程MySQL导入到本地MySQL数据库
I.MX6UL核心模块使用连载-以太网测试 (七)
Create a future and enjoy extraordinary | gbase Nantah General Motors unveiled opengauss Developer Day 2022
SQLyog数据导入导出图文教程
1205 Lock wait timeout exceeded; try restarting transaction处理
(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud
Design and driver transplantation of matrix keyboard circuit of Ti am335x industrial control module
AttributeError: ‘Document‘ object has no attribute ‘pageCount‘
Worthington产气荚膜梭菌神经氨酸酶的特征及测定
vite 本地运行首次进入页面加载慢问题