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
}
}
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.