Arash
Arash

Reputation: 1826

.NET CORE unit test - mockRepo.Setup for repository always return NULL

I have a simple method which is using an interface to call the repository, method is working just fine but whenever I try to mock Login method and return something fake, it just return null for me.

my method

[HttpPost]
public ActionResult<LoginResultViewModel> Login([FromBody] UserViewModel model)
{
    var result = new LoginResultViewModel();
    var loginResult = _authRepository.Login(new User
    {
        Email = model.Email,
        Password = model.Password
    });
    result.IsLogin = loginResult.IsLogin;

    result.User = new UserViewModel
    {
        Email = loginResult.User.Email,
        Id = loginResult.User.Id,
    };

    return Ok(result);
}

And here is my test

[Fact]
public void Login_Return_User_With_Token()
{
    // Arrange
    var _fakeLoginResult = new LoginResult
    {
        Token = "Fake token",
        User = new User
        {
            Email = "Fake email"
        }
    };
    var fakeUser = new User { Email = "Fake Email", Password = "Fake Password" };
    var fakeUserViewModel = new UserViewModel { Email = "Fake Email", Password = "Fake Password" };
    
    var mockRepo = new Mock<IAuthRepository>();
    var controller = new AuthController(mockRepo.Object);
    mockRepo.Setup(repo => repo.Login(fakeUser)).Returns(_fakeLoginResult);
    

    // Act
    var methodResult = controller.Login(fakeUserViewModel);

    // Assert
    var testResult = methodResult.Result as OkObjectResult;
    Assert.Equal(_fakeLoginResult, testResult.Value);
}

Error catch in result.IsLogin = loginResult.IsLogin; because loginResult is null.

Upvotes: 1

Views: 906

Answers (1)

Nkosi
Nkosi

Reputation: 247521

By default, if a mocked member's setup (expectation) does not match what was actually invoked, it will return null.

In this case the setup (expectation) was using a specific User reference (instance)

var fakeUser = new User { Email = "Fake Email", Password = "Fake Password" };

//...

mockRepo.Setup(repo => repo.Login(fakeUser)) //<--
    .Returns(_fakeLoginResult);

that did not match the one created when it was invoked, and therefore returned null.

var loginResult = _authRepository.Login(new User //<-- new User created here
{
    Email = model.Email,
    Password = model.Password
});

Use argument matchers It.IsAny<User>()

mockRepo.Setup(repo => repo.Login(It.IsAny<User>())).Returns(_fakeLoginResult);

if the argument can be any User value when invoking the mock

or use It.Is<User>(predicate)

mockRepo
    .Setup(repo => repo.Login(It.Is<User>(u => u.Email == "Fake Email" && u.Password == "Fake Password")))
    .Returns(_fakeLoginResult);

if the arguments needs to match specific User characteristics.

Reference MOQ Quickstart: Matching Arguments

Upvotes: 1

Related Questions