(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 );
}