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

简介: (一)安装结果(SetupResult) 有时候在模拟对象中需要一个方法的返回值,而不在意这个方法是否被调用。就可以通过安装结果(SetupRestult)来设置返回值,而绕开期望安装,且可以使用多次。

(一)安装结果(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<T>委托可见:

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<Customer>()

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<T>)

 

 

相同

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开头,且以!号结束。

 

博客园大道至简

http://www.cnblogs.com/jams742003/

转载请注明:博客园

目录
相关文章
|
关系型数据库 MySQL PHP
php wampserver的使用配置
本文介绍了WampServer在Windows系统下的配置和使用方法,包括如何修改PHP时区为中国标准时区PRC、更改Apache服务器端口号以避免冲突、设置起始页以及如何创建和管理虚拟目录。通过这些步骤,用户可以更有效地在本地环境中开发和测试PHP程序。
php wampserver的使用配置
人工智能 缓存 前端开发
6271 19
人工智能 JavaScript 开发工具
3238 4
缓存 JavaScript Shell
1534 1
开发工具 Swift git
997 1
Shell API 调度
838 2
|
13天前
|
存储 弹性计算 缓存
阿里云服务器租赁费用:新版租赁收费标准及活动报价参考
本文更新了2026年阿里云全系列云服务器租赁活动报价,所有特惠资源均可前往阿里云活动中心选购,整体覆盖从个人入门到企业级高性能场景的全梯度需求。其中轻量应用服务器主打极致性价比,2核2G峰值200M带宽配置每日10点、15点限时抢购价仅38元/年,2核4G配置379元/年起;高性价比的经济型e实例、通用算力型u2i实例覆盖2核4G至4核32G全档位,适配开发测试与中小型企业业务;搭载英特尔至强6处理器的第九代c9i企业级实例算力较上代提升20%,支撑高并发生产环境,不同实例规格价差清晰,用户可根据自身业务负载与预算灵活选型。
2112 121
阿里云服务器租赁费用:新版租赁收费标准及活动报价参考
|
14天前
|
人工智能 程序员 API
Codex 接入 DeepSeek-V4-Flash:还能补上识图,提供两套方案
Codex 接入 DeepSeek-V4-Flash 怎么配?本文覆盖 CLI 与桌面端,再用 qwen3-vl-flash 补识图,两套方案可直接照做
1751 13
安全 机器人 API
583 2

热门文章

最新文章