我如何使用 xUnit 为这个 cqrs 编写单元测试?

问题描述 投票:0回答:1
public class UserCommandHandler : IUsersCommandHandler
{
    private readonly IServiceScopeFactory _serviceScopeFactory;
    private readonly ILogger<UsersCommandHandler> _logger;
    private readonly IUnitOfWork _unitOfwork;
    private readonly IRepository<Users> _usersRepository;

    public UsersCommandHandler(IServiceScopeFactory serviceScopeFactory, ILogger<UsersCommandHandler> logger, IUnitOfWork unitOfwork, IRepository<Users> userRepository)
    {
        _serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        _unitOfwork = unitOfwork ?? throw new ArgumentNullException(nameof(unitOfwork));
        _userRepository = usersRepository ?? throw new ArgumentNullException(nameof(userRepository));


    }

    
    public async Task<CreateUserResponseModel> Handle(CreateUserModel request, CancellationToken cancellationToken)
    {
        Users entity = request.ToEntity();
        Users addedEntity = _userRepository.Insert(entity);
        await _unitOfwork.SaveChangesAsync(CancellationToken);
        CreateUserResponseModel model = addedEntity.ToModelCreate();
        return model;    


    }
}

有人可以帮我如何为此使用 xUnit 编写单元测试吗?我被困住了。

我尝试编写单元测试,但我不能,我需要一个解决方案

asp.net unit-testing xunit
1个回答
0
投票

您没有提供任何必需的信息,所以我猜是 UnitOfWork 和 UserRepository 内部的代码,您需要更改此测试

我将 ServiceScopeFactory 和 Logger 作为 null 传递,因为我们没有在 Handel 中使用它

总的来说,测试是这样的

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;

public class UserCommandHandlerTests
{

        private readonly DbContextOptions<MyDbContext> dbContextOptions;

        private MyDbContext _inMemoryDbContext;

        public UserCommandHandlerTests()
        {
            dbContextOptions = new DbContextOptionsBuilder<MyDbContext>()
                .UseInMemoryDatabase(databaseName: "MyDatabaseName")
                .Options;
        }


    [Fact]
    public async Task UserCommandHandler_Should_AddUserToDatabase()
    {
        // Arrange

        _inMemoryDbContext = new MyDbContext(dbContextOptions);

        var unitOfWork=new UnitOfWork(_inMemoryDbContext);

        var userRepository=new Repository<User>();

        var userCommandHandler=new UserCommandHandler(null,null,unitOfWork,userRepository);

        var createUserData=new CreateUserModel()
        {
            Id=Guid.NewGuid(),
            Name="fake name"
        };

        // Act
        var createUserResponse  =await userCommandHandler.Handle(createUserData);

        var user=await _inMemoryDbContext.Users.FirstOrDefaultAsync(x=>x.Id==createUserData.Id);

        // Assert
        Assert.NotNull(createUserResponseModel);
        Assert.NotNull(user);
        Assert.IsType<CreateUserResponseModel>(createUserResponse);
        Assert.IsType<User>(user);

        // Clean
        _inMemoryDbContext.Dispose();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.