Expect call is not working on mock

468 Views Asked by At

I am writing test for CheckPassWord() I presume the Expect call is not behaving as expected on my userMangerMock.

//CheckPassword returns true if the parameter matches to the exsting user.
//Existing user is obtained by GetUser() by internal call
     bool passWordMatch = userMangerMock.CheckPassword(userInfo.Id, userInfo.Password);

CheckPassWord() internally calls GetUser(), Since GetUser() needs more deeper internal calls I decided to return a stubUser I believe implementation of Expect() is sufficient for that. Note that the following call var userInfo = userMangerMock.GetUser("TestManager");is returning stubUser. But , CheckPassword() call I am assuming the stubUser is not returned thus the test failing.

Correct me if any bug in the following UT.

   //Use TestInitialize to run code before running each test
    [TestInitialize()]
    public void MyTestInitialize()
    {
        CreateUser();
    } 
    private static IUser _stubUser;

    public void CreateUser()
    {
        IUserFactory iUserFactory = new UserFactory();

        UserParams parameters = new UserParams();
        parameters.Password = "TestPassword123!";
        parameters.UserID = "TestManager";
        _stubUser = iUserFactory.CreateUser(parameters);
    }    

    /// <summary>
    ///A test for CheckPassword
    ///</summary>
    [TestMethod( )]
    public void CheckPasswordTest()
    {
        // !!! Below I've used WhenCalled() to show you that correct  
        // expectation is called based on argument type, just see in debugger
        IUserManager userMangerMock = MockRepository.GenerateMock<IUserManager>();
        userMangerMock.Expect(x => x.GetUser(Arg<string>.Is.Anything))
                  .WhenCalled((mi) =>
                  {
                      Debug.WriteLine("IUserManager - string parameter");
                  })
                  .Return(_stubUser);

        var userInfo = userMangerMock.GetUser("TestManager");

        bool passWordMatch = userMangerMock.CheckPassword(userInfo.Id, userInfo.Password);
        userMangerMock.VerifyAllExpectations();

        Assert.AreEqual(true, passWordMatch);
    }

    /// <summary>
    /// Returns true if password matches the user
    /// </summary>
    public bool CheckPassword(string userId, string password)
    {
        if (userId == null)
        {
            throw new ArgumentNullException("userId");
        }

        IUser user = GetUser(userId);
        if (user == null)
        {
            throw new UserManagementException(UserManagementError.InvalidUserId);
        }

        return (user.Password == password);
    }
1

There are 1 best solutions below

0
Jason Evans On

A couple of things I noticed in your test:

userMangerMock.VerifyAllExpectations();

This will always pass since you are manually calling GetUser() in the test code itself:

var userInfo = userMangerMock.GetUser("TestManager");

So you can actually remove this verify call, since it's not needed.

It feels like your unit test does not seem to provide any value to you, since it's asserting on a hard-coded mock object.

 var userInfo = userMangerMock.GetUser("TestManager");

    bool passWordMatch = userMangerMock.ChePassword(userInfo.Id, userInfo.Password);

    Assert.AreEqual(true, passWordMatch);

If userInfo is a reference to the stub object _stubUser then you're unit test can be refactored to:

bool passWordMatch = userMangerMock.CheckPassword(_stubUser.Id, _stubUser.Password);

Assert.AreEqual(true, passWordMatch);