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();
// assert
// Assert.Equal(5, result.Count());
// fluent assert
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);
// Arrange ------------------------------------------------------------------------------
// 创建一个包含10个实体的 TodoList
var todoLists = Builder<TodoList>.CreateListOfSize(10).Build().ToList();
// 函数定义:Task<List<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>> predicate);
// 模拟入口参数 模拟返回参数
// GetAllAsync(Expression<Func<TEntity, bool>> predicate); Task<List<TEntity>>
todoListRepository.GetAllAsync(Arg.Any<Expression<Func<TodoList, bool>>>()).Returns(todoLists);
// 函数定义:string GetUserId();
// 模拟返回参数
// string
claimService.GetUserId().Returns("1234567890");
// Act ------------------------------------------------------------------------------
var result = await sut.GetAllAsync();
//Assert
Console.WriteLine(result.Count());
// dotnet run --project ./tests/N-Tier.Application.UnitTests.Learn.Console