在测试项目中为登录操作方法创建单元测试时出错

时间:2019-10-30 11:23:31

标签: unit-testing xunit

我正在尝试使用UserManager和SignInManager编写用于Login的单元测试用例。

尽管当我的代码到达控制器的这一行时,我正在传递正确的用户凭据,但我得到的结果为null。  var result = await _signInManager.PasswordSignInAsync(model.userNameGst,model.Password,false,false);

我的测试控制器如下

public class UserManagementControllerTest
{
    private AccountController _userController;       
    private Mock<UserManager<UserField>> _mockUserManager;
    private Mock<SignInManager<UserField>> _mockSignInManager;               

    public UserManagementControllerTest()
    {
        var mockConfig = new Mock<IConfiguration>();
        var mockEmail = new Mock<IEmailSender>();
        var userStoreMock = new Mock<IUserStore<UserField>>();

        _mockUserManager = new Mock<UserManager<UserField>>(userStoreMock.Object,
            null, null, null, null, null, null, null, null);

        var contextAccessor = new Mock<IHttpContextAccessor>();
        var userPrincipalFactory = new Mock<IUserClaimsPrincipalFactory<UserField>>();

        _mockSignInManager = new Mock<SignInManager<UserField>>(_mockUserManager.Object,
            contextAccessor.Object, userPrincipalFactory.Object, null, null, null);

        _userController = new AccountController(_mockUserManager.Object, _mockSignInManager.Object, mockConfig.Object, mockEmail.Object);
    }

    [Fact]
    public async Task Test_LoginWhenCalledRetunrsResponse()
    {
        var mockUser = new Users()
        {
            UserName = "PepsiInd123",
            Password = "1234",
            confirmpassword = "1234",
            userNameGst = "PepsiInd123",
        };           
        var result = await _userController.Login(mockUser);
        Assert.IsType("".GetType(), result.GetType());            

    }
}

碰到

var result = await _signInManager.PasswordSignInAsync(model.userNameGst, model.Password, false, false);

我得到的结果为空。因此,我的测试用例失败了。 我在做什么错?

0 个答案:

没有答案