怪物奇妙物语

宇宙无敌超级美少男的怪物奇妙物语

首页 新随笔 联系 管理
  819 随笔 :: 0 文章 :: 2 评论 :: 16万 阅读

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
posted on   超级无敌美少男战士  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2023-10-26 Node.js 绿色版 多版本 手动管理 windows 安装
点击右上角即可分享
微信分享提示