RhinoMock入门(7)——Do,With和Record-playback

做者: 梅桦 发表于 2010-05-11 15:50 原文连接 阅读: 86 评论: 0html

(一)Do(delegate)程序员

有时候在测试过程当中只返回一个静态的值是不够的,在这种状况下,Do()方法能够用来在方法调用时添加自定义的行为。通常来讲,Do()方法会替换方法调用。它的返回值会从模拟的调用中返回(即便是有异常发生也是这样)。Do()的参数委托委托的方法的签名须和方法的签名匹配。只有当签名匹配时才能生效,且一个匹配生效一次。测试

看官方给出的例子:网站

public   class  Speaker
{
    
private   readonly   string  firstName;
    
private   readonly   string  surname;
    
private  INameSource nameSource;

    
public  Speaker( string  firstName,  string  surname, INameSource nameSource)
    {
        
this .firstName  =  firstName;
        
this .surname  =  surname;
        
this .nameSource  =  nameSource;
    }

    
public   string  Introduce()
    {
        
string  name  =  nameSource.CreateName(firstName, surname);
        
return   string .Format( " Hi, my name is {0} " , name);
    }
}

  

public   interface  INameSource
{
    
string  CreateName( string  firstName,  string  surname);
}

 

如今演讲者和名字分开在两个类中。而后进行自我介绍,介绍时要介绍本身的姓名,即 FirstName+LastName 。在介绍中要用到 InameSource 中的 CreateName 方法,接下来将会模拟这个接口,而经过其它的方法来替代。

 

[Test]
public   void  SayHelloWorld()
{
    MockRepository mocks 
=   new  MockRepository();
    INameSource nameSource 
=  mocks.DynamicMock < INameSource > ();

    Expect.Call(nameSource.CreateName(
null null ))
          .IgnoreArguments()
          .Do(
new  NameSourceDelegate(Formal));

    mocks.ReplayAll();
    
string  expected  =   " Hi, my name is Ayende Rahien " ;
    
string  actual  =   new  Speaker( " Ayende " " Rahien " , nameSource).Introduce();
    Assert.AreEqual(expected, actual);
}

delegate   string  NameSourceDelegate( string  first,  string  surname); 

private   string  Formal( string  first,  string  surname)
{
    
return  first  +   "   "   +  surname;
}

 

看上段测试的粗体部分。this

Do参数是委托类型,其中这个委托类型委托的方法的签名要和模拟对象中指望的要替换的方法的签名一致,即:spa

private   string  Formal( string  first,  string  surname)
string  CreateName( string  firstName,  string  surname);

 

二者相匹配。code

而后当对演讲者构造时:
new Speaker("Ayende", "Rahien", nameSource)orm

会对演讲者三个域进行赋值视频

private   readonly   string  firstName;
private   readonly   string  surname;
private  INameSource nameSource;

 

接下来进行介绍时,调用方法:htm

public   string  Introduce()
{
    
string  name  =  nameSource.CreateName(firstName, surname);
    
return   string .Format( " Hi, my name is {0} " , name);
}

 

而这个方法则由Do方法的委托参数委托的方法来替代:

private   string  Formal( string  first,  string  surname)
{
    
return  first  +   "   "   +  surname;
}

 

返回FirstName+空格+LastName 

(二)With

流畅式的指望和验证语法。什么是流畅式?先看例子:

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

    
string  strTemp = string .Empty; 

    With.Mocks(mocks)
        .Expecting(
          
delegate
          {
             Expect.Call(customer.ShowTitle(
"" )).Return( " with 语句 " );
          })
        .Verify(
         
delegate
          {
             strTemp 
=  customer.ShowTitle( "" );
          });

    Assert.AreEqual(
" with 语句 " , strTemp);
}

 

这就是所谓的流畅式。经过匿名委托来实现。若是在匿名委托中完成则会隐匿调用ReplayAll()mocks.VerifyAll()

若是要启用次序,则可以使用:ExpectingInSameOrder,例如:

[Test]
public   void  TestFluent()
{
    MockRepository mocks 
=   new  MockRepository();
    var customer 
=  mocks.DynamicMock < ICustomer > ();
    
string  strTemp = string .Empty;
    With.Mocks(mocks).ExpectingInSameOrder(
        
delegate
        {
           Expect.Call(customer.ShowTitle(
"" )).Return( " with 语句 " );
           Expect.Call(customer.Unid).Return(
1 );
        })
       .Verify(
       
delegate
       {
          strTemp 
=  customer.ShowTitle( "" );
          
int  i  =  customer.Unid;
       });

    Assert.AreEqual(
" with 语句 " , strTemp);
}

 

With语句的隐式使用

With能够隐式的建立Mock实例,并自动调用VerifyAll方法。

[Test]
public   void  TestWithMocker()
{
    With.Mocks(
        
delegate
        {
          var customer 
=  Mocker.Current.DynamicMock < ICustomer > ();
          Expect.Call(customer.ShowTitle(
"" )).Return( " with 语句 " );
          Mocker.Current.ReplayAll();

          Assert.AreEqual(
" with 语句 " , customer.ShowTitle( "" ));
        });
}

 

这里才看出With确实很流畅。

下边说一下由显式建立Mock实例来代替隐式建立:

[Test]
public   void  TestWithMockerr()
{
    MockRepository mocks 
=   new  MockRepository();
    With.Mocks(mocks,
       
delegate
       {
          var customer 
=  Mocker.Current.DynamicMock < ICustomer > ();
          Expect.Call(customer.ShowTitle(
"" )).Return( " with 语句 " );

          Mocker.Current.ReplayAll();

          Assert.AreEqual(
" with 语句 " , customer.ShowTitle( "" ));
       });
}

 

没多大区别。在使用Mocker.Current时,不能在嵌套中使用,由于这是个全局的,而With.Mocks会重写Mocker.Current

(三)Record-PlayBack

Rhinomock支持一种经过Using语句来进行录制回放的方式。

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

    
using  (mocks.Record())
    {
        Expect.Call(customer.ShowTitle(
"" )).Return( " 录制回放 " );
    }

    
using  (mocks.Playback())
    {
        Assert.AreEqual(
" 录制回放 " , customer.ShowTitle( "" ));
    }
}

 

 

评论: 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

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

相关文章
相关标签/搜索