tags:

views:

183

answers:

1

Hi, I need help with a testmethod im trying to write...

I need to test that a user can show his profile, however i encounter an error when i try to use my mocked GetProfileFromUserName method. The methods returns null. What I Dont understand is that i have a similar method named GetEmail, which basically does the same and works.

This is the code for retrieving the profile, which doesnt work:

mockUserRepository.Setup(gp => gp.GetProfileFromUserName(userProfile.UserName)).Returns(new Profile { ProfileID = userProfile.ProfileID });

And this is the code for retrieving the email, which works.

mockUserRepository.Setup(em => em.GetEmail(new MockIdentity("JohnDoe").Name)).Returns("[email protected]");

And this is a snippet of the method the mock calls and returns null on instead of a profile:

public ActionResult ShowProfile()
    {
        var profile = _userRepository.GetProfileFromUserName(User.Identity.Name);

What am i doing wrong? If i replace the userProfile.UserName in the GetProfileFromUserName to It.IsAny();

+1  A: 

If it returns null, it means that your Setup didn't match the actual call. Check that the userProfile.UserName contains the correct value at the Setup line.

Also, to detect the unmatched calls create your mockUserRepository with the MockBehavior.Strict option.

Hope this helps.

Yacoder