-2

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);
    }
Srikanth
  • 980
  • 3
  • 16
  • 30
  • 3
    Code you've posted does not look like real unit test - it never call anything except mocks. Please verify your unit test code and also show implementation you are trying to test. – Alexei Levenkov Sep 26 '16 at 05:44
  • Its clearly calling CheckPassword at bool passWordMatch = userMangerMock.CheckPassword(userInfo.Id, userInfo.Password); I have updated CheckPassword() implementation. – Srikanth Sep 26 '16 at 08:12
  • userManagerMock.CheckPassword() is calling the method on the mock object. You aren't calling any real code at that point. – Pedro Jan 04 '17 at 16:13

1 Answers1

1

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);
Jason Evans
  • 28,906
  • 14
  • 90
  • 154