xunit测试
dotnet cli 创建测试项目
| dotnet new xunit -o tests/N-Tier.Application.UnitTests.Learn |
| dotnet sln add tests/N-Tier.Application.UnitTests.Learn |
| dotnet add tests/N-Tier.Application.UnitTests.Learn reference src/N-Tier.Application |
测试指定项目
| dotnet test tests/N-Tier.Application.UnitTests.Learn |
测试
测试类没有任何依赖注入-直接new
测试类: WatherForecastServiceTests.cs
| using N_Tier.Application.Services.Impl; |
| |
| namespace N_Tier.Application.UnitTests.Services; |
| |
| public class WeatherForecastServiceTests |
| { |
| private readonly WeatherForecastService _sut; |
| |
| public WeatherForecastServiceTests() |
| { |
| _sut = new WeatherForecastService(); |
| } |
| |
| [Fact] |
| public async Task GetAsync_Should_Return_List_With_Only_Five_ElementsAsync() |
| { |
| var result = await _sut.GetAsync(); |
| Assert.Equal(5, result.Count()); |
| } |
| } |
被测试类: WeatherForecastService.cs
| using N_Tier.Application.Helpers; |
| using N_Tier.Application.Models.WeatherForecast; |
| |
| namespace N_Tier.Application.Services.Impl; |
| |
| public class WeatherForecastService : IWeatherForecastService |
| { |
| private readonly List<string> _summaries; |
| |
| public WeatherForecastService() |
| { |
| _summaries = new List<string> |
| { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; |
| } |
| |
| public async Task<IEnumerable<WeatherForecastResponseModel>> GetAsync() |
| { |
| await Task.Delay(500); |
| |
| return Enumerable.Range(1, 5).Select(index => new WeatherForecastResponseModel |
| { |
| Date = DateTime.Now.AddDays(index), |
| TemperatureC = RandomGenerator.GenerateInteger(-20, 55), |
| Summary = _summaries[RandomGenerator.GenerateInteger(0, _summaries.Count)] |
| }); |
| } |
| } |
测试类 : Flent Assertions > WatherForecastServiceTests.cs
| using FluentAssertions; |
| using N_Tier.Application.Services.Impl; |
| namespace N_Tier.Application.UnitTests.Services; |
| public class WeatherForecastServiceTests |
| { |
| private readonly WeatherForecastService _sut; |
| public WeatherForecastServiceTests() |
| { |
| _sut = new WeatherForecastService(); |
| } |
| [Fact] |
| public async Task GetAsync_Should_Return_List_With_Only_Five_ElementsAsync() |
| { |
| var result = await _sut.GetAsync(); |
| |
| |
| |
| |
| |
| result.Should().HaveCount(5); |
| } |
| } |
测试类有依赖注入
| dotnet add .\tests\N-Tier.Application.UnitTests.Learn\ package FluentAssertions |
| dotnet run --project ./tests/N-Tier.Application.UnitTests.Learn.Console |
N-Tier.Application.UnitTests.Learn.Console > Program.cs
| using AutoMapper; |
| using Microsoft.Extensions.Configuration; |
| using NSubstitute; |
| using N_Tier.Application.MappingProfiles; |
| using N_Tier.Application.Services.Impl; |
| using N_Tier.DataAccess.Repositories; |
| using N_Tier.Shared.Services; |
| using FizzWare.NBuilder; |
| using N_Tier.Core.Entities; |
| using System.Linq.Expressions; |
| using FluentAssertions; |
| |
| var mapper = new MapperConfiguration(cfg => |
| { |
| cfg.AddMaps(typeof(TodoItemProfile)); |
| }).CreateMapper(); |
| |
| var configurationBody = new Dictionary<string, string> |
| { |
| { "JwtConfiguration:SecretKey", "Super secret token key" } |
| }; |
| |
| var configuration = new ConfigurationBuilder().AddInMemoryCollection(configurationBody).Build(); |
| |
| var todoListRepository = Substitute.For<ITodoListRepository>(); |
| |
| var claimService = Substitute.For<IClaimService>(); |
| |
| var sut = new TodoListService(todoListRepository, mapper, claimService); |
| |
| |
| |
| |
| var todoLists = Builder<TodoList>.CreateListOfSize(10).Build().ToList(); |
| |
| |
| |
| |
| todoListRepository.GetAllAsync(Arg.Any<Expression<Func<TodoList, bool>>>()).Returns(todoLists); |
| |
| |
| |
| |
| |
| claimService.GetUserId().Returns("1234567890"); |
| |
| |
| var result = await sut.GetAllAsync(); |
| |
| |
| |
| Console.WriteLine(result.Count()); |
| |
| |
| |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2023-10-26 Node.js 绿色版 多版本 手动管理 windows 安装