Contents
How do you mock in UnitOfWork?
2 Answers
- Add an interface IRepository , and implement this with your GenericRepository class.
- Add in interface IUnitOfWork and implement this with your UnitOfWork class.
- The IUnitOfWork interface only refers to IRepository , not to GenericRepository
What does mocking mean in Mockito?
The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called. We can use it to create mock class fields, as well as local mocks in a method.
How to create unit testable applications in MVC?
In the UnitOfWork’s default constructor the BooksRepository class will be instantiated. The UnitOfWork class will point to the real BooksRepository implementation. All the actions of the Controller will use the real BooksRepository class to perform actual CRUD operations.
Can You unit test a unit of work?
I’ve found it to work very well. Albeit for unit testing purposes, you wouldn’t want to test your unit of work (unless verifying certain objects are added/deleted etc.). You would instead test repositories with predefined responsibilities – usually defined within context (ex. patient appointments).
Is it possible to mock a unit of work?
Unfortunately your GenericRepository is tightly coupled with your context, and your UnitOfWork implementation if tightly coupled with your repositories. This makes it impossible to mock it.
How to test a unit of work in C #?
You would insert a mock of the DBContext and then verify that the SaveChanges method is called on commit. [Test] public void Will_call_save_changes () { var mockContext = new Mock (); var unitOfWork = new UnitOfWork (mockContext.Object); unitOfWork.Commit (); mockContext.Verify (x => x.SaveChanges ()); } Is this answer outdated?