During project development, all the crud functions in the data access layer have passed the test. Now there is a manager class in the logical service layer, and the crud function in the Manager also calls the crud function in the data access layer, without other logic. When we test the crud function of Manager, do we actually need to access the database? The answer is no.
We only need to test whether the crud function in the Manager has called the function in the data access layer!
Here we can use Moq for testing. The Moq plug-in is required. Here: Click the open link
Because Moq is only useful for virtual or abstract functions, you need to add an interface for the class in the data access layer. Shape
public interface IRepository { Test AddEntity(Test t); }public class TestRepository : IRepository { private Test TestEntity; public TestRepository() { this.TestEntity = new Test(); } public Test AddEntity(Test t) { if (t == null) { return null; } TripEntities te = new TripEntities(); DbSet testSet = te.Set<Test>(); testSet.Add(t); te.SaveChanges(); return t; } }
Here, only addentity has code, and other crud functions are not pasted.
Now let's look at the related code of the manager class.
public class Manager { private IRepository repository; public Manager() : this(new TestRepository()) { } public Manager(IRepository tr) { this.repository = tr; } public Test AddEntity(Test t) { if (t == null) { return null; } return this.repository.AddEntity(t); } }
Note that the manager has two constructors. The constructor with parameters uses the constructor injection method in dependency injection. The constructor Parameters specify services for service objects.
Now, write the test function. You can use the test tool provided by vs or nunit. Here I am using nunit: Click to open the link. Let's look at the Code:
Using Moq; using nunit. framework; [testfixture] public class unittestmanager {[test] public void testaddentity () {var repository = new mock <irepository> (); test expected = new test {SID = 3, name = "hell", notes = "go",}; repository. setup (A =>. addentity (it. isany <Test> ())). returns (expected ). verifiable (); manager m = new manager (repository. object); test T = new test (); var actual = m. addentity (t); repository. verify (A =>. addentity (it. isany <Test> (), times. atleastonce (), "at least once"); assert. areequal (expected. sid, Actual. sid); assert. areequal (expected. name, Actual. name); assert. areequal (expected. notes, Actual. notes );}}
Now we can test that the addentity function of manager calls the function in repository at least once. Of course, the returned actual does not actually access the database but our simulation data. Here we still test its value to ensure that the addentity function of the manager has no exception.