示例
1、CRUDxUnitTests项目中安装依赖包FluentAssertion
NuGet\Install-Package FluentAssertions -Version 6.12.0
2、更新CountriesServiceTest.cs中的Assert
using AutoFixture;
using Entities;
using EntityFrameworkCoreMock;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using ServiceContracts;
using ServiceContracts.DTO;
using Services;
using System;
using System.Collections.Generic;
namespace CRUDxUnitTests
{
public class CountriesServiceTest
{
private readonly ICountriesService _countriesService;
private readonly IFixture _fixture;
public CountriesServiceTest()
{
var countriesInitialData = new List<Country>() { };
DbContextMock<ApplicationDbContext> dbContextMock =
new DbContextMock<ApplicationDbContext>(new DbContextOptionsBuilder<ApplicationDbContext>().Options);
ApplicationDbContext dbContext = dbContextMock.Object;
dbContextMock.CreateDbSetMock(c => c.Countries,countriesInitialData);
_countriesService = new CountriesService(dbContext);
_fixture = new Fixture();
}
#region AddCountry
//When CountryAddRequest is null, it should throw ArgumentNullException
[Fact]
public async Task AddCountry_NullCountry()
{
//Arrange
CountryAddRequest? request = null;
//Act
Func<Task> action = async () =>
{
await _countriesService.AddCountry(request);
};
//Assert
await action.Should().ThrowAsync<ArgumentNullException>();
}
//When the CountryName is null , it should throw ArgumentException
[Fact]
public async Task AddCountry_CountryNameIsNull()
{
//Arrange
CountryAddRequest request = new CountryAddRequest()
{
CountryName = null
};
//Act
Func<Task> action = async () =>
{
await _countriesService.AddCountry(request);
};
//Assert
await action.Should().ThrowAsync<ArgumentException>();
}
//When the CountryName is duplicate, it should throw ArgumentException
[Fact]
public async Task AddCountry_DuplicateCountryName()
{
//Arrange
CountryAddRequest request1 = _fixture.Build<CountryAddRequest>()
.With(c => c.CountryName, "USA").Create();
CountryAddRequest request2 = _fixture.Build<CountryAddRequest>()
.With(c => c.CountryName, "USA").Create();
//Act
Func<Task> action = async () =>
{
await _countriesService.AddCountry(request1);
await _countriesService.AddCountry(request2);
};
//Assert
await action.Should().ThrowAsync<ArgumentException>();
}
//When you supply proper country name, it should inset (add) the country to the existing list of countries
[Fact]
public async Task AddCountry_ProperCountryName()
{
//Arrange
CountryAddRequest request = _fixture.Build<CountryAddRequest>()
.With(c => c.CountryName, "ChaoXian").Create();
//Act
CountryResponse countryResponse = await _countriesService.AddCountry(request);
List<CountryResponse> countryResponseFromGetAllCountries = await _countriesService.GetAllCountries();
//Assert
countryResponse.CountryId.Should().NotBe(Guid.Empty);
countryResponseFromGetAllCountries.Should().Contain(countryResponse);
//Assert.Contains比较的时候使用了objA.Equals(objB),是引用类型的比较,不是值的比较
//所以需要在CountryResponse类中重写Equals
}
#endregion
#region GetAllCountries
//The list of countries should be empty by default(before adding any countries)
[Fact]
public async Task GetAllCountries_EmptyList()
{
//Acts
List<CountryResponse> countryResponsesList = await _countriesService.GetAllCountries();
//Assert
countryResponsesList.Should().BeEmpty();
}
//judge if less countries is added
[Fact]
public async Task GetAllCountries_AddFewCountries()
{
//Arrange
List<CountryAddRequest> countryAddRequestList = new List<CountryAddRequest>()
{
_fixture.Build<CountryAddRequest>()
.With(c => c.CountryName, "USA").Create(),
_fixture.Build<CountryAddRequest>()
.With(c => c.CountryName, "UK").Create()
};
//Act
List<CountryResponse> countryResponseLsit = new List<CountryResponse>();
foreach (CountryAddRequest item in countryAddRequestList)
{
countryResponseLsit.Add(await _countriesService.AddCountry(item));
}
List<CountryResponse> actualCountryResponseList = await _countriesService.GetAllCountries();
//Assert
//read each element from countryResponseLsit
//foreach (CountryResponse expected_country in countryResponseLsit)
//{
// Assert.Contains(expected_country, actualCountryResponseList);
//}
actualCountryResponseList.Should().BeEquivalentTo(countryResponseLsit);
}
#endregion
#region GetCountryByCountryId
//If we supply null as CountryId, it should return null as CountryResponse
[Fact]
public async Task GetCountryByCountryId_NullCountryId()
{
//Arrange
Guid? countryId = null;
//Act
CountryResponse? countryResponse = await _countriesService.GetCountryByCountryId(countryId);
//Assert
countryResponse.Should().BeNull();
}
//If we supply a valid country id, it should return the matching country details as CountryResponse object
[Fact]
public async Task GetCountryByCountryId_ValidCountryId()
{
//Arrange
CountryAddRequest? countryAddRequest = _fixture.Build<CountryAddRequest>()
.With(c => c.CountryName, "China").Create();
CountryResponse? countryResponseAdd = await _countriesService.AddCountry(countryAddRequest);
//Act
CountryResponse? countryResponseGet = await _countriesService.GetCountryByCountryId(countryResponseAdd.CountryId);
//Assert
countryResponseGet.Should().BeEquivalentTo(countryResponseAdd);
}
#endregion
}
}
Gitee获取源码: