views:

34

answers:

1

I am new to Moq and need to know if I am doing this right.

In AccountController.cs I have this:

      int id = _userRepository.GetProfileFromUserName(userName).ProfileID;

UserRepository is mocked, but ProfileID comes from DataContext, so I did this in my AccountControllerTests.cs:

      mockUserReposository.Setup(gp => gp.GetProfileFromUserName(userName)).Returns(new Profile { ProfileID = 1 });

This way I get the id variable to be equal to 1, and ensure that ProfileID doesn't use the one from DataContext when called in AccountController.cs

Is this the right way to do it? Or do I somehow need to mock my whole Profile table from Linq to SQL?

+1  A: 

Depends what you're testing. If you're looking to Fake the GetProfileFromUserName for the purposes of sticking in a desired Profile, it looks fine.

Can you edit in more of your test so people can give more complete insights - the only answer given how much you've said is It Depends :D

Ruben Bartelink