RhinoMock入門(6)——安裝結果和約束

做者: 梅樺 發表於 2010-05-11 11:17 原文連接 閱讀: 92 評論: 0html

(一)安裝結果(SetupResult程序員

有時候在模擬對象中須要一個方法的返回值,而不在乎這個方法是否被調用。就能夠經過安裝結果(SetupRestult)來設置返回值,而繞開指望安裝,且可使用屢次。從依賴的角度來講是這樣的:方法a(或屬性)被方法b使用,而在其它的位置c處方法a又會被使用,而在c處使用以前,不保證是否在b處使用且修改了方法a的返回值。意思就是保證方法a的返回結果是固定的,是忽略它的依賴,而在它該用的位置使用它恆定的值。安裝結果能夠達到這種效果。 測試

public   class  Customer
{
    
public   virtual   int  DescriptionId{ get ; set ;}
    
public   virtual   void  PrintDescription()
    {
        DescriptionId
= 1 ;
    }
}

 

屬性 DesriptionId 被方法 PrintDescription() 依賴。

 

[Test]
public   void  TestSetupResult()
{
    MockRepository mocks 
=   new  MockRepository();
    var customer 
=  mocks.DynamicMock < Customer > ();
    SetupResult.For(customer.DescriptionId).Return(
10 ); 

    Expect.Call(
delegate  { customer.PrintDescription(); }).Repeat.Times( 2 );

    mocks.ReplayAll();

    customer.PrintDescription();
    customer.PrintDescription();

    Assert.AreEqual(
10 , customer.DescriptionId);
}

 

從這段測試中能夠看到,對customerDescriptionId屬性進行告終果安裝,只讓這個屬性返回10。而在隨後對依賴它的方法進行了指望安裝,且能夠被調用2次。但DescriptionId的值還是10網站

Expect.Call(delegate { customer.PrintDescription(); }).Repeat.Times(2);this

這句是對不帶返回值的方法進行指望安裝,固然可使用Lambda來進行。這個匿名方法就是一個不帶參數,沒有返回值的委託,這個就至關於Action<>,經過lambda就是:()=>customer.PrintDescription(),完整就是:spa

Expect.Call(()=>customer.PrintDescription()).Repeat.Times(2);code

關於匿名方法和Action委託可見:orm

http://www.cnblogs.com/jams742003/archive/2009/10/31/1593393.html視頻

http://www.cnblogs.com/jams742003/archive/2009/12/23/1630737.htmlhtm

 

安裝結果有兩種方法:

ForOnFor在上邊已經使用,On的參數是mock object。對於上邊的示例中的粗體部分用On來實現爲:

SetupResult.On(customer).Call(customer.DescriptionId).Return( 10 );

 

這個也能夠經過指望的選項來實現。例如:

Expect.Call(customer.DescriptionId).Return( 10 )
      .Repeat.Any()
      .IgnoreArguments();

其中的粗體部分,能夠屢次使用,且忽略參數。

(二)約束(Constraints

約束用來對指望的參數進行規則約束。系統提供了大量內建的約束方法,固然也能夠自定義。這裏直接貼一張官網給出的列表,一目瞭然: 

約束

說明

例子

接受的值

拒絕的值

Is

任何

Is.Anything()

{0,"","whatever",null, etc}

Nothing Whatsoever

等於

Is.Equal(3)

3

5

不等於

Is.NotEqual(3)

null, "bar"

3

Is.Null()

null

5, new object()

不爲無

Is.NotNull()

new object(), DateTime.Now

null

指定類型

Is.TypeOf(typeof(Customer))

or Is.TypeOf()

myCustomer, new Customer()

null, "str"

大於

Is.GreaterThan(10)

15,53

2,10

大於等於

Is.GreaterThanOrEqual(10)

10,15,43

9,3

小於

Is.LessThan(10)

1,2,3,9

10,34

小於等於

Is.LessThanOrEqual(10)

10,9,2,0

34,53,99

匹配

Is.Matching(Predicate)

 

 

相同

Is.Same(object)

 

 

不相同

Is.NotSame(object)

 

 

Property

等於值

Property.Value("Length",0)

new ArrayList()

"Hello", null

Property.IsNull

("InnerException")

new Exception

("exception without

 inner exception")

new Exception

("Exception

with inner Exception",

 new Exception("Inner")

不爲無

Property.IsNotNull

("InnerException")

new Exception

("Exception with inner Exception",

new Exception("Inner")

new Exception

("exception without

inner exception")

List

集合中包含這個元素

List.IsIn(4)

new int[]{1,2,3,4},

 new int[]{4,5,6}

new object[]{"",3}

集合中的元素(去重)

List.OneOf(new int[]{3,4,5})

3,4,5

9,1,""

等於

List.Equal(new int[]{4,5,6})

new int[]{4,5,6},

new object[]{4,5,6}

new int[]{4,5,6,7}

Text

以…字串開始

Text.StartsWith("Hello")

"Hello, World",

"Hello, Rhino Mocks"

"", "Bye, Bye"

以…字串結束

Text.EndsWith("World")

"World",

"Champion Of The World"

"world", "World Seria"

包含

Text.Contains("or")

"The Horror Movie...",

"Either that or this"

"Movie Of The Year"

類似

Text.Like

("rhino|Rhinoceros|rhinoceros")

"Rhino Mocks",

"Red Rhinoceros"

"Hello world", "Foo bar",

Another boring example string"

 

例子:

[Test]
public   void  TestConstraints()
{
    MockRepository mocks 
=   new  MockRepository();
    var customer 
=  mocks.DynamicMock < ICustomer > ();

    Expect.Call(customer.ShowTitle(
"" ))
          .Return(
" 字符約束 " )
          .Constraints(Rhino.Mocks.Constraints
                           .Text.StartsWith(
" cnblogs " )); 

    mocks.ReplayAll();
    Assert.AreEqual(
" 字符約束 " , customer.ShowTitle( " cnblogs my favoured " ));
}

 

它的意思就是若是參數以cnblogs開頭,則返回指望值。能夠比較一下Moq的參數約束設置方法:

http://www.cnblogs.com/jams742003/archive/2010/03/02/1676197.html 

除了上述方法外,rhinomock約束還支持組合,即與,非,或。還以上例進行:

[Test]
public   void  TestConstraints()
{
    MockRepository mocks 
=   new  MockRepository();
    var customer 
=  mocks.DynamicMock < ICustomer > ();

    Expect.Call(customer.ShowTitle(
"" ))
          .Return(
" 字符約束 " )
          .Constraints(
               Rhino.Mocks.Constraints.Text.StartsWith(
" cnblogs "
            
&&  Rhino.Mocks.Constraints.Text.EndsWith( " ! " )
    ); 

    mocks.ReplayAll();
    Assert.AreEqual(
" 字符約束 " , customer.ShowTitle( " cnblogs my favoured! " ));
}

 

參數的條件就是以cnblogs開頭,且以!號結束。

 

評論: 0 查看評論 發表評論

程序員找工做,就在博客園

最新新聞:
· 電子商務網站之信任度(2010-10-09 17:02)
· 馬雲:管理的核心在於「抓住人性的本真」(2010-10-09 16:52)
· 另外一 Windows Phone Live 主頁截圖現身 Windows Phone 7 視頻(2010-10-09 16:38)
· 谷歌首名員工:公司成功歸結於運氣不錯(2010-10-09 16:32)
· 神奇小子Geohot帶着「limera1n」迴歸(2010-10-09 16:29)

編輯推薦:遠離.NET

網站導航:博客園首頁  我的主頁  新聞  閃存  小組  博問  社區  知識庫

相關文章
相關標籤/搜索