Learn MVC's rental site (iv)-Implement service tiers and conduct unit tests

Source: Internet
Author: User

In the previous < study on the MVC Rental website (iii)-writing the Eneity class and creating a database >, documenting the process of writing the Eneity class and creating the database in Codefirst way, next to the service layer implementation, and before the start of the follow-up work, Start with a full unit test.

For a long time, has been to write a lot of bugs and distress, here after the unit test, pleasantly surprised to find that this is not a good way to ensure the quality of the code, although it will take extra hours, but decided to use the unit test in the work and learning practice.

First, implement service layer

1. in order to reduce the coupling between modules and layers, the IService interface layer is added to the service layer, a table corresponds to a service class, and corresponds to a method of implementing a set of IService interfaces. For example, the Background administrator table t_adminuser the corresponding relationship is:

T_adminuser Table->adminuserservice->iadminuserservice

The methods defined in the Iadminuserservice interface are:

These methods are then implemented in the Adminuserservice class.

2. Configure AUTOFAC

The service classes are in the Zsz.service project, and these service classes implement the interfaces in the Zsz.iservice. You will then configure the use of IOC container AUTOFAC to add code in the Global.asax:

About the IOC and AUTOFAC very much do not understand, first use up, slowly understand it, The general function is to configure Zsz.service for the service assembly to be used, call IService directly in the controller, so that controller and service layer decoupling, decoupling is of course a good thing.

In order to selectively allow AUTOFAC to manage the service, an empty interface Iservicesupport is used to make the identity, only the Iservicesupport interface will be injected. But for the isassignablefrom here need to learn, by the way can give the number of public numbers, hey ~


Second, Unit testing

After the service is implemented, the unit test starts, vs comes with the project template for the unit test, and a series of tags are provided under namespace Microsoft.VisualStudio.TestTools.UnitTesting, such as [TestClass], [ TestMethod] And so on, I've only used two of these. Unit testing needs to be systematically studied, but in recent practice, it has been found that at least the appropriate unit test notation, a test class corresponding to a unit test class, one method will have multiple test cases, each of which writes a method that facilitates quick positioning to the wrong code after vs gives the test results. But each case has a method that causes too many methods, and #region wraps a method that corresponds to all the case methods associated with the method being measured.

As for Adminuserservice. GetAll (long Cityid) test, think of the following three test cases:


Finally, there is a special assert,assert.throwsexception to check whether the specified exception type is returned, as follows:

Assert.throwsexception<argumentexception> (() = Atservice.getattachments (2));

Note:

The content of the course to free Peng Network (www.rupeng.com), focusing on college students employment online education platform;

ASP. NET MVC course http://www.rupeng.com/News/9/640.shtml

Learn MVC's rental site (iv)-Implement service tiers and conduct unit tests

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.