(1) Order (Ordered)
In rhinomock, you can call methods in sequence. By default, method calls are not ordered. If recording is performed in order, the method must be called in the same order as the recording time.
See:
Public interface icustomer
{
String showtitle (string Str );
Int unid {Get; set ;}
String customername {Get; set ;}
String address {Get; set ;}
}
Test:
Public void testnoorder ()
{
Mockrepository mocks = new mockrepository ();
Icustomer customer = mocks. strictmock <icustomer> ();
// There is no order by default.
Ct. Call (customer. unid). Return (1 );
Ct. Call (customer. customername). Return (" ");
Ct. Call (customer. Address). Return ("Shandong ");
Mocks. replayall ();
Assert. areequal ("", customer. customername );
Assert. areequal (1, customer. unid );
Assert. areequal ("Shandong", customer. Address );
}
When using the sequence:
Public void testorder ()
{
Mockrepository mocks = new mockrepository ();
Icustomer customer = mocks. strictmock <icustomer> ();
Using (mocks. Ordered ())
{
Ct. Call (customer. unid). Return (1 );
Ct. Call (customer. customername). Return (" ");
Ct. Call (customer. Address). Return ("Shandong ");
}
Mocks. replayall ();
Assert. areequal ("", customer. customername );
Assert. areequal (1, customer. unid );
Assert. areequal ("Shandong", customer. Address );
}
If the call is not performed in the expected sequence, an error occurs and an exception is thrown.
This order can be used flexibly. For example, a mock can be ordered, and then a mock is expected to not be executed in order after the condition is reached. Note: exit the sequence before playback.
(2) simulate Delegation
Define delegation first:
Public Delegate void dothing (string strmsg );
Then simulate the delegation:
[Test]
Public void testdelegate1 ()
{
Mockrepository mocks = new mockrepository ();
VaR oo = mocks. dynamicmock <dothing> ();
Oo ("");
Mocks. replayall ();
Oo ("");
Mocks. verifyall ();
}
There are two system-defined delegation func <tresult> and action <t>
The preceding example is a delegate with a return value. The latter does not have a return value. Now, the preceding example is implemented using action <t>.
[Test]
Public void testdelegate2 ()
{
Mockrepository mocks = new mockrepository ();
VaR oo = mocks. dynamicmock <action <string> ();
Oo ("");
Mocks. replayall ();
Oo ("");
Mocks. verifyall ();
}
Another example of func is a delegate with a returned value:
[Test]
Public void testdelegatefunc ()
{
Mockrepository mocks = new mockrepository ();
VaR oo = mocks. dynamicmock <func <string, string> ();
Ct. Call (OO (""). Return ("ABC ");
Mocks. replayall ();
Assert. areequal ("ABC", Oo (""));
}
Another example:
Public Class Customer
{
Func <string, string> _ fun;
Public customer (func <string, string> fun)
{
_ Fun = fun;
}
Public void dosomething (string strmsg)
{
Console. writeline (_ fun (strmsg ));
}
}
Test:
[Test]
Public void testdelegatefunc ()
{
Mockrepository mocks = new mockrepository ();
VaR oo = mocks. dynamicmock <func <string, string> ();
Ct. Call (OO (""). Return ("ABC ");
Mocks. replayall ();
VaR customer = new customer (OO );
Customer. dosomething ("");
}
For the two types of delegation, see: http://www.cnblogs.com/jams742003/archive/2009/10/31/1593393.html