Rhinomock entry-sequence and Delegation

Source: Internet
Author: User

(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

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.