views:

215

answers:

1

Hi

I am having problems with moq again and not sure what I did wrong this time.

So I am going through the debugger step by step and I notice even though in my Mock I set

ResetPassword to return "hey it does not seem to

Here is part of my unit test:

var membershipMock = new Mock<MembershipProvider>();



           var user = new Mock<MembershipUser>();

           user.SetupGet(x => x.Email).Returns("Email");
           user.Setup(x => x.ResetPassword("test")).Returns("hey");

           membershipMock.Setup(m => m.GetUser("chobo2", false)).Returns(user.Object);



           authentication.Authenticate.Provider = membershipMock.Object;

           // Act

           var actual = authentication.PasswordRecoveryStep2("chobo2","2z");

My code

  MembershipUser userName = Provider.GetUser(user, false);

                string newPassword = userName.ResetPassword(securityAnswer);

 Mail.To = userName.Email;

Provider is a property that gets set with the mockup object as you and see. The email part gets filled fine. Just ResetPassword does not return what it should. It just give me null.

Thanks

+3  A: 

If "2z" is your security answer Moq will not return "hey" as it's setup to only return "hey" for string inputs that match "test". The following works perfectly for me:

public class AuthenticationHelper {
    readonly MembershipProvider _provider;

    public AuthenticationHelper(MembershipProvider provider) {
        _provider = provider;
    }

    public string PasswordRecoveryStep2(string userName, string recoveryAnswer) {
        MembershipUser user = _provider.GetUser(userName, false);
        string newPassword = user.ResetPassword(recoveryAnswer);
        return newPassword;
    }
}

Which passes the following test...

[TestFixture]
public class AuthHelperTests {
    Mock<MembershipProvider> memberShipProvider;
    Mock<MembershipUser> user;

    [SetUp]
    public void Init() {
        memberShipProvider = new Mock<MembershipProvider>();
        user = new Mock<MembershipUser>();
        user.SetupGet(u => u.Email)
            .Returns("[email protected]");
        user.Setup(u => u.ResetPassword("secret"))
            .Returns("test2");
        memberShipProvider
            .Setup(prov => prov.GetUser("test", false))
            .Returns(user.Object);
    }

    [Test]
    public void WillResetPasswordByCallingProvider() {
        var helper = new AuthenticationHelper(memberShipProvider.Object);
        string newPassword = helper.PasswordRecoveryStep2("test", "secret");
        Assert.AreEqual("test2", newPassword);
        memberShipProvider.Verify(p => p.GetUser("test", false));
        user.Verify(u => u.ResetPassword("secret"));
    }
}
sighohwell
Nice it just tested and it seems to work. I did not know it was looking for a match I thought it did not care what the stuff was.Now if someone could just tell me when I would know when a methods from .Net is Virtual,Has an Interface and extra so I can know if I can mock it up or not.I am trying to do some stuff with MailMessage(sys.net) and I am not sure if I have to write my own interface and wrapper for it.
chobo2
If you want it not to care just use It.IsAny<string>() instead of "test".
sighohwell