Moq Testing General Class

Source: Internet
Author: User
Tags class manager

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.