I have never written Unit tests, but I want to use them in my new project. Here is the example of my code:
public class UnitOfWork : IDisposable, IUnitOfWork
{
private IDbContext context = new MyDbContext();
private IUserRepository userRepository;
public IUserRepository UserRepository
{
get
{
if (this.userRepository== null)
{
this.userRepository = new UserRepository (context);
}
return userRepository;
}
}
}
public class UserController : Controller
{
private IUnitOfWork unitOfWork;
public UserController(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public ActionResult GetSomeUsers ()
{
var users = unitOfWork.UserRepository.GetSomeUsers(/*Some condition*/);
return View(users);
}
}
Lets assume that var users = unitOfWork.UserRepository.GetSomeUsers(/*Some condition*/);
has some complex query inside it.
First thing I want to know is how to unit test that query, do I have to create a separate database which has predefined records and query against that database?
The second thing is how to test only controller method, with mocked UserRepository?
If you can write a short example or provide me some tutorial link it would be great.