Unit Test Learning Series (i)---nmock object and Database simulation introduction

Source: Internet
Author: User

The unit test is to check and verify the minimum testable unit of a system, the rules of key points and the key logic of the system can be set up for a unit test, but for some tests with uncertain behavior object or the test uncertainty factor of database operation is higher, The initial unit tests are often discarded in the middle and late stages because of changes in database tables or changes in data or changes in object structure, and so on. Therefore, in order to set up a unit test and maximize its function, we must first solve the uncertainty of object behavior. You can consider the modularity of these behaviors by relying on interfaces to isolate them, and unit tests only need to emulate those interfaces and inject them into the class that requires unit testing, so that you can decouple the indeterminate object from the unit test without relying on it.        The function of NMock is realized by simulating the properties, methods, and events of the interface, thus implementing the Unit test without having to rely on the interface implementation Class (the principle is that the object is created by the dynamic proxy at runtime). So it seems that to use NMock for unit testing assistance, your system must be interface-oriented programming, that is, the class you want to test external dependencies must be interface dependent. At present I think of the appropriate application scenario has two:
    1. The MVP Architecture mode system establishes the unit test for the controller. The MVP architecture pattern separates the view layer and the business logic in an interface, for the development of the system we only need to define the view interface without concern for its implementation, you can write the control layer, and define the view interface specific UI logic can be implemented slowly, because there is no specific implementation, Unit tests that you want to perform on the controller can be simulated on the view with the help of NMock.
    2. DDD Domain drives the establishment of unit tests for domain layers. The call for warehousing in the DDD domain-driven implementation requires only an interface defined at the domain level, and then the implementation can be implemented without concern, and the domain layer is dependent on the storage interface for data persistence. You can use NMock to simulate storage when you want to test classes such as the aggregation root of the domain layer.
The above two is my study of the NMock after the two application scenarios, but also the next two blog preparation specific introduction. Personally, as long as the interface-oriented programming, unit testing can basically use NMock for auxiliary testing. Basic Environment Construction I use the NMock is the 3 version of the Nmock3,nmock's official website is: http://nmock3.codeplex.com/download directory as follows
can support FRAMEWORK4 and Framework3.5,tutorials is a tutorial project, I am using the Fx3.5 version of, download NMock3 Fx 3.5 Unzip only one DLL, unit test project reference can be
The example code first learns some basic code of use, the next two to learn the next two instances of the application scenario.
First, you must first create the base factory:
Mockfactory _factory = new Mockfactory ();

A simulation of an interface can call the following code:
mock<ilist>  _mock = _factory. Createmock<ilist> ();
The _mock at this time is not an IList object, _mock. Mockobject is the mock object of IList (which can be said to be an implementation class)
The expected fetch for a property can be used in the following code
_view.expects.one.getproperty (v = v.id, 1000);
Where _view is a created mock object with an id attribute, which is 1000 when accessing _view.mockobject.id.
For the simulation of an event, the following code is available:
_view.expects.one.eventbinding (v = v.init + = null);

For the simulation of a method, the following code is available:
AddressRp.Expects.One                . Method (d = d.saveaddress (null))                . With (adress)                . Would (Return.value (result));

Specific usage methods will be described in detail in the next two articles.

Unit Test Learning Series (i)---nmock object and Database simulation introduction

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.