Reputation: 39
I'm trying to test GetUserDetails
from UserTest class. test
user has data, but test always returns null for getUser
. Do I write test correctly? Any suggestion how to use service method here in a rigth way
[TestClass]
public class UserTest
{
IUserService _userService;
[TestInitialize]
public void Setup()
{
_userService = new Mock<IUserService>().Object;
}
[TestMethod]
public void getUserInfo()
{
var getUser = _userService.GetUserDetails("test"); //it's not null, user has data
Assert.IsNotNull(getUser); //always null
}
}
Upvotes: 1
Views: 2227
Reputation: 35175
If you are testing GetUserDetails
you should not mock the service it is part of.
Your test should look more like this:
[TestMethod]
public void TestGetUser()
{
var mockLogger = new Mock<ILogger>(MockBehaviour.Strict);
var mockRepo = new Mock<IMyRepo>(MockBehaviour.Strict);
var beingTested = new UsersService(
logger: mockLogger.Object
userRepo: mockRepo.Object,
...);
mockRepo.Setup( r => r.GetUserFromDb("test")).Returns( new UserDetails(...));
var getUser = beingTested.GetUserDetails("test");
Assert.IsNotNull(getUser);
}
Upvotes: 3