Let's talk about the simulated test and the simulation.

Source: Internet
Author: User

Let's talk about the simulated test and the simulation.
Simulate a method

Controller:

Namespace WebApplication1.Controllers

{

Public class TestController: Controller

{

Public TestController (IService service)

{

This. Service = service;

}

Public IService Service {get; set ;}

// GET: Test

Public virtual ActionResult Index ()

{

Var result = this. Service. Foo ();

ViewBag. res = result;

Return View ();

}

}

}

Test code:

Namespace MyTest

{

Public class Class1

{

[Fact]

Public void IndexTest ()

{

Var mock = new Mock <IService> ();

Mock. Setup (s => s. Foo (). Returns ("I am simulating ");

TestController controller = new TestController (mock. Object );

ViewResult result = controller. Index () as ViewResult;

Assert. Equal ("I am simulating", result. ViewBag. res );

}

}

}

Using Mock to implement the method specified in IService, we implemented the Foo method. When I call the Index method in TestController, the Foo method in is actually a method I simulated using Mock, not a method in a real project.

Simulate a method without returning a value

Controller:

Namespace WebApplication1.Controllers

{

Public class TestController: Controller

{

Public TestController (IService service)

{

This. Service = service;

}

Public IService Service {get; set ;}

// GET: Test

Public ActionResult Index ()

{

This. Service. Foo ();

Return View ();

}

}

}

Test code

Namespace MyTest

{

Public class Class1

{

[Fact]

Public void IndexTest ()

{

Var mock = new Mock <IService> ();

Mock. Setup (s => s. Foo ());

TestController controller = new TestController (mock. Object );

ViewResult result = controller. Index () as ViewResult;

Mock. Verify (s => s. Foo ());

Assert. NotNull (result );

}

}

}

When the simulated method does not return a value, we need to determine whether the method is executed. You can use mock. Verify (); to determine whether the method is called. The method is used as above. You can also specify whether the verification is called by setting the simulation method. For example:

Use Verifiable ()

Mock. Setup (s => s. Foo (). Verifiable ();

Mock. Verify ();

Ps: VerifyAll () is used to verify all methods. You do not need to specify

Parameter Verification

Use It. Is <> () to verify the parameter, for example

It. Is <int> (I => I = 1). The parameter Is of the int type of 1.

Mock. VerifyAll ();

Attribute

Set the default value for the property:

Mock. Setup (s = & gt; s. Lala). Returns ("12390 ")

Or

Mock. SetupProperty (s => s. Lala, "12398 ");

Verification:

Mock. SetupSet (s => s. Lala = "1232"); // you can specify the expected value.

Mock. VerifyAll (); // verify whether the value assignment in the Controller is consistent with the expected value

Or

Action <IService> action = I => {I. Lala = "1232 ";};

Mock. VerifySet (action );

Ps: verification is a constant value and can also be replaced by methods in the It class, such

It. Is <string> (p => p = "33 ")

Verify whether the API is called:

Mock. VerifyGet (s => s. Lala );

Or

Mock. Setup (s => s. Lala). Verifiable ();

Mock. Verify ();

Or

Mock. Setup (s => s. Lala)

Mock. VerifyAll ();

Trace property value:

Mock. SetupProperty (s => s. Lala, "00"); // you can set the property to be tracked and modified.

Var obj = mock. Object;

Obj. Lala = "90"; // modify attributes of a trail

Ps: mock. Setup (s => s. Lala). Returns ("12390"), setting properties will not be tracked

Simulate the protected method:

Var mock = new Mock <Bar> ();

Mock. Protected (). Setup <string> ("Ox"). Returns ("321 ");

Var res = mock. Object;

Using mock Setup directly cannot simulate the protected method. mock. Protected () is used to solve this problem.

Ps: Reference namespace using Moq. Protected;

Test the private method:

Bar bar = new Bar ();

Var barPrivate = new PrivateObject (bar );

Var result = barPrivate. Invoke ("Pp ");

Ps: Reference Microsoft. visual Studio. qualityTools. unitTestFramework. dll, path: C: \ Program Files (x86) \ Microsoft Visual Studio 14.0 \ Common7 \ IDE \ PublicAssemblies \ (varies depending on the vs version), you need to manually add reference

References:

Http://www.cnblogs.com/darrenji/p/3869602.html#a

Http://www.cnblogs.com/haogj/archive/2011/07/22/2113496.html

Http://www.cnblogs.com/gossip/archive/2012/05/16/2503477.html

 

Related Article

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.