RhinoMock入门(6)——安装结果和约束

本文详细介绍了如何利用Rhino Mocks的安装结果和约束功能,实现对方法返回值的固定设置及对依赖参数的规则约束。通过示例展示了如何在测试场景中灵活运用这些特性,确保代码的预期行为得到验证。

作者: 梅桦 发表于 2010-05-11 11:17 原文链接 阅读: 92 评论: 0

(一)安装结果(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);

这句是对不带返回值的方法进行期望安装,当然可以使用Lambda来进行。这个匿名方法就是一个不带参数,没有返回值的委托,这个就相当于Action<>,通过lambda就是:()=>customer.PrintDescription(),完整就是:

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

关于匿名方法和Action委托可见:

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

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

 

安装结果有两种方法:

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

网站导航:博客园首页  个人主页  新闻  闪存  小组  博问  社区  知识库

转载于:https://my.oschina.net/jams742003/blog/8852

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值