Rhinomock entry-Mock repository, expected

Source: Internet
Author: User

(1)Mockrepository

The main class of rhinomock is the mock repository, which is used to create mock objects, recording, replay, verification, and so on.

(1) create a mock

Mockrepository mock = new mockrepository ();

(2) create a mock object

The purpose of mock is to create a mock object, expect it, record and replay it. There are many methods to create a mock object. In previous versions, the following methods were used:

Mockrepository FAC = new mockrepository ();
VaR customer = FAC. createmock <icustomer> ();

The createmock method is out of date, and the factory method is used:

Mockrepository FAC = new mockrepository ();
VaR customer = FAC. strictmock <icustomer> ();

 

You can also create a static Factory:

VaR customer = mockrepository. generatemock <icustomer> ();

 

(3) Recording

All mock frameworks use the record/replay mode, but some are displayed, some are implicit, and rhinomock is displayed.

· Record/replay traditional recording and replay:

[Test]
Public void testrr ()
{

Mockrepository FAC = new mockrepository ();
VaR customer = FAC. strictmock <icustomer> ();
 

Customer. CT (P => P. showtitle (""). Return ("");
Customer. Replay ();
Assert. areequal ("", customer. showtitle (""));
}

 

· Record, using mode

Public void testrr ()
{
Mockrepository FAC = new mockrepository ();
VaR customer = FAC. strictmock <icustomer> ();
Using (FAC. Record ())
{
Customer. CT (P => P. showtitle (""). Return ("");
}

Assert. areequal ("", customer. showtitle (""));
}

 

 

· Create a mock object using the static factory method:

Public void testrr ()
{
VaR customer = mockrepository. generatemock <icustomer> ();
Customer. CT (P => P. showtitle (""). Return ("");
Assert. areequal ("", customer. showtitle (""));
}

 

(4) Verification

Used to verify whether the mock object is expected to be true.

· Verify, used to verify whether all expectations of the mock object meet

· Verifyall is used to verify whether all expectations of all mock objects meet

 

(2)Secondary CTClass

Set expectation for methods of classes with returned values

Call: Provides expectations for methods or properties

· Call <t> (T ignored)

· Call (Action actiontoexecute)

 

Example:

[Test]
Public void testcall ()
{
Mockrepository mock = new mockrepository ();
VaR mockobject = mock. strictmock <icustomer> ();
Ct. Call (mockobject. showtitle (string. Empty). Return ("cannot be blank ");

Reverse CT. Call (mockobject. unid). Return (30 );
Mock. replayall ();
Assert. areequal ("cannot be blank", mockobject. showtitle (""));
Assert. areequal (30, mockobject. unid );
}

 

 

Lambda expressions

[Test]
Public void testcall ()
{
Mockrepository mock = new mockrepository ();
VaR mockobject = mock. strictmock <icustomer> ();
Mockobject. Expect CT (P => P. showtitle (""). Return ("cannot be blank ");
Mockobject. Expect (P => P. unid). Return (30 );

Mock. replayall ();
Assert. areequal ("cannot be blank", mockobject. showtitle (""));
Assert. areequal (30, mockobject. unid );
}

 

 

Let's look at the using method.

[Test]
Public void testcallusing ()
{
Mockrepository mock = new mockrepository ();
VaR mockobject = mock. strictmock <icustomer> ();

 

Using (Mock. Record ())
{
Mockobject. Expect CT (P => P. showtitle (""). Return ("cannot be blank ");
Mockobject. Expect (P => P. unid). Return (30 );
}

Assert. areequal ("cannot be blank", mockobject. showtitle (""));
Assert. areequal (30, mockobject. unid );
}

 

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.