NMock Learning Series (ii) Application of---nmock in unit test of MVP architecture system

Source: Internet
Author: User

This article has learned some of the basic concepts and Code of NMock, as well as the possible two scenarios, which begins with the first application scenario---NMock in the MVP architecture mode. MVP Architecture Mode concept is relatively simple, mainly in the form of interface to isolate the coupling between the view and controller, specifically for the introduction of the MVP model, please search for learning. The next prerequisite for this study is that the reader understands the architecture pattern of the MVP and understands the decoupling of the view interface.

Application scenarios based on the MVP model, the business logic is written and the view is built separately, and the view simply defines the interface for the business controller to make dependent calls. So if the view is not implemented yet, the premise that we want to unit test each business controller must be to simulate the behavior and properties of the view interface. Then we can use the NMock to do the auxiliary test. The following starts the direct exit code instance:
First, the view interface is defined as follows:
        Public interface iviewbase        {             int ID {get;}             String Name {get; set;}     Event EventHandler Init;        }
The controller is defined as follows
        public class Presenterbase{public string Status {get; protected set;} Public Presenterbase (Iviewbase view) {view = view; Status = "Constructed";} Protected Iviewbase view{get, private set;} public void Bindeventsinternal () {view.init + = View_init; Bindevents ();} protected virtual void bindevents () {}private void View_init (object sender, EventArgs e) {Status = "Init";}}

The specific data model I did not define, because the model is not considered for the time being in this application scenario. To establish a unit test for the controller method Bindeventsinternal, we have to define a class that implements the interface iviewbase specifically, and we can simulate it through NMock, the code is as follows:
        Private Mockfactory _factory;        [TestInitialize]        public void TestInitialize ()        {            _factory = new Mockfactory ();          }        [TestMethod]        public void Testmockmvp ()        {            mock<iviewbase> _view = _factory. Createmock<iviewbase> ();            Set Event            _view.expects.one.eventbinding (v = v.init + = null);    Simulation Method            //If the generic type is as follows            //_view.expects.one.eventbinding< type > (v = v.init + = null);                Presenterbase presenter = new Presenterbase (_view.mockobject);            Assert.isnotnull (presenter);        }

The simulated _view.mockobject is equivalent to an instance of a specific class that implements the Iviewbase.

NMock Learning Series (ii) Application of---nmock in unit test of MVP architecture system

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.