Answer the question
In order to leave comments, you need to log in
How to do unit tests with Moq?
Hello! The question is, for example, there is a repository
public class UserRepository
{
private EFDbContext _context;
public UserRepository(EFDbContext context)
{
_context = context;
}
public IReadOnlyCollection<User> GetAll()
{
return _context.User.ToList();
}
}
public class UserSvc
{
private readonly IUserRepository userRepo;
public UserSvc(IUserRepository userRepo)
{
this.userRepo = userRepo
}
public IReadOnlyCollection<User> GetAll()
{
return userRepo.GetAll();
}
}
Answer the question
In order to leave comments, you need to log in
Depends on what you want to test. In this case, it is quite possible that you should already write integration tests, and not unit. Then you don't need to wet.
The question is not entirely clear, if you want to test the UserRepository, hide the context behind the interface and write a mock.
Then you can emulate the "bad" behavior of the object (context), and based on this, expand the logic of the repository. The situation is similar to the UserSvc that you actually described (I have to lock IUserRepository to test a pure service. ).
At the initial stage, I would recommend that you write mocks yourself, then use various frameworks.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question