208 Fluent Assertions 01

示例

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获取源码:

https://gitee.com/huang_jianhua0101/asp.-net-core-8.git

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄健华Yeah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值