我正在尝试对书面方法进行单元测试,但我遇到了问题。

这是我的UnityConfig类:

public class UnityConfig
{
    #region Unity Container
    private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        RegisterTypes(container);
        return container;
    });

    public static IUnityContainer GetConfiguredContainer()
    {
        return container.Value;
    }
    #endregion

    public static void RegisterTypes(IUnityContainer container)
    {
        container.RegisterInstance(AutoMapperConfig.Initialize());
        container.RegisterType<IActiveDirectoryUserService, ActiveDirectoryUserService>();
        container.RegisterType<IUserRepository, InMemoryUserRepository>();
        container.RegisterType<IUserService, UserService>();
    }
}


测试方法:

public async Task RegisterAsync(string name, string email, string firstName, string lastName, string password)
{
    var user = await _userRepository.GetBynameAsync(name);

    if (user != null)
    {
        throw new Exception($"User {name} already exists.");
    }
    else
    {
        var ActiveDirectoryUser = await _ActiveDirectoryUserService.GetBynameAsync(name);

        if (ActiveDirectoryUser == null)
        {
            throw new Exception($"User {name} does not exist in Active Directory");
        }
        else
        {
            var salt = new Guid().ToString("N");
            user = new User(name, ActiveDirectoryUser.Email, ActiveDirectoryUser.FirstName, ActiveDirectoryUser.LastName, password, salt);
            await _userRepository.AddAsync(user);
        }
    }
}


测试方法:

[Test]
public async Task Test()
{
    var userRepositoryMock = new Mock<IUserRepository>();
    var activeDirectoryUserMock = new Mock<IactiveDirectoryUserService>();
    var mapperMock = new Mock<IMapper>();

    var userService = new UserService(userRepositoryMock.Object, activeDirectoryUserMock.Object, mapperMock.Object);

    await userService.RegisterAsync("name", "[email protected]", "first name", "last name", "password");

    userRepositoryMock.Verify(x => x.AddAsync(It.IsAny<User>()), Times.Once);
}


经过测试的方法在外部测试中工作正常,但是当我运行测试时,它失败了。我在调试模式下运行测试,发现它没有进入

var user = await _userRepository.GetBynameAsync(name);

var ActiveDirectoryUser = await _ActiveDirectoryUserService.GetBynameAsync(name);


知道为什么它在测试期间没有进入这些方法吗?我需要在UnityConfig中添加一些条目以使其工作吗?

-编辑

private static ISet<User> _users = new HashSet<User>
{
    new User("user1", "[email protected]", "firstnam1", "lastname1", "pass", "salt"),
    new User("user2", "[email protected]", "firstnam2", "lastname2", "pass", "salt"),
    new User("user3", "[email protected]", "firstnam3", "lastname3", "pass", "salt")
};

public async Task<IEnumerable<User>> GetAllAsync()
    => await Task.FromResult(_users);

最佳答案

发生的情况是,模拟不知道如何从异步调用返回,因此,当代码尝试等待它们时,模拟将失败。它不一定与您的DI容器有关。它不在此单元测试中。

做出一些假设,因为没有提供有关测试中使用的类型的足够信息。

给定被测方法,让我们假设您具有这样的接口

public interface IUserRepository {
    Task<User> GetByNameAsync(string name);
    Task AddAsync(User user);
}
public interface IActiveDirectoryUserService {
    Task<AdUserAccount> GetByNameAsync(string name);
}
public class AdUserAccount {
    public  string Email { get; set; }
    public  string FirstName { get; set; }
    public  string LastName { get; set; }
}


请注意返回Task的方法。

您将需要设置模拟程序,以了解如何处理异步调用并允许任务完成。

[Test]
public async Task Test() {
    //Arrrange
    var name = "name";
    var email = "[email protected]";
    var firstname = "first name";
    var lastname = "last name";

    var userRepositoryMock = new Mock<IUserRepository>();
    //Testing that it returns no user for that name so return null from async call
    userRepositoryMock.Setup(_ => _.GetByNameAsync(name)).ReturnsAsync((User)null);
    //Need this to make sure async call runs to completion
    userRepositoryMock.Setup(_ => _.AddAsync(It.IsAny<User>())).Returns(Task.FromResult((object)null));

    var activeDirectoryUserMock = new Mock<IActiveDirectoryUserService>();
    //fake account for mock AD call
    var adUser = new AdUserAccount {
        Email = email,
        FirstName = firstname,
        LastName = lastname
    };
    //Need fake ad account to return from async call
    activeDirectoryUserMock.Setup(_ => _.GetByNameAsync(name)).ReturnsAsync(adUser);

    var mapperMock = new Mock<IMapper>();

    var userService = new UserService(userRepositoryMock.Object, activeDirectoryUserMock.Object, mapperMock.Object);

    //Act
    await userService.RegisterAsync(name, email, firstname, lastname, "password");

    //Assert
    userRepositoryMock.Verify(x => x.AddAsync(It.IsAny<User>()), Times.Once);
}

关于c# - Unity和NUnit,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44763233/

10-12 04:40