0

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.

Aleksa
  • 2,976
  • 4
  • 30
  • 49
  • Why don't you check some open source project tests and use them as example? There're tons of .NET projects hosted in GitHub – Matías Fidemraizer Dec 10 '15 at 11:14
  • A good start might be to look into the difference between integration tests and unit tests. There are many advantages and disadvantages of each that I won't go into here. But it will hopefully go someway to answering your question about whether to include a database or not in your tests. – TomDoesCode Dec 10 '15 at 13:10

1 Answers1

0
  • do I have to create a separate database which has predefined records and query against that database?

If you wish to implement integration tests, then yes create a database with test data and query against it. Integration tests are very important and useful. On the other hand if you wish to create unit tests then you need to mock the database access somehow because a unit test doesn't interact with the environment but it just test the functionality itself.

See e.g. this answer to get more info about the difference between unit and integration tests.


  • how to test only controller method, with mocked UserRepository?

Assuming the method GetSomeUsers retuns a List of strings the unit test could look like this. HTH

[TestMethod]
public void GetSomeUsers_WhenCalled_RetunsView()
{
    // Arrange
    Mock<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();
    Mock<IUserRepository> userRepositoryMock = new Mock<IUserRepository>();

    List<string> listOfFakeUsers = new List<string> { "John Doe", "etc." };
    userRepositoryMock.Setup(ur => ur.GetSomeUsers()).Returns(listOfFakeUsers);

    unitOfWorkMock.SetupGet(uow => uow.UserRepository).Returns(
                                                          () => userRepositoryMock.Object);
    UserController controller = new UserController(unitOfWorkMock.Object); 

    // Act
    ActionResult result = controller.GetSomeUsers();

    // Assert
    Assert.IsInstanceOfType(result, typeof(ViewResult));
}
Community
  • 1
  • 1
Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51