如何在Rhino Mocks 3.6中将Expect设置为扩展方法 [英] How to set Expect to a Extension method in Rhino Mocks 3.6

查看:86
本文介绍了如何在Rhino Mocks 3.6中将Expect设置为扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好

我有一个类,并且有一个关联的扩展方法.

I have a class and it has an associated extension method.

public class Person{
    public int ID {get;set}
    public string Name {get;set}
}

扩展方法:

public static class Helper {
   public static int GetToken(this Person person){
       int id = 0;
       //Something here is done to read token from another service...
       return id;
   }
}

现在我正在尝试使用Rhino并测试此方法

Now I am trying to use Rhino and test this method

public void readPersonToken(int personId) {

   var person = Person.GetPersonInfo(personId);//we consume a service here
   Console.Writeline(person.GetToken());//get token is consuming another service

}

假设我正在编写测试,并且已经有一个调用GetPersonInfo()的接口

Supposing I am writing my test and have already an interface that calls GetPersonInfo()

var _interface = MockRepository.GenerateMock<IMyInterface>();

并且主要期望是

_interface.Expect(x => x.GetPersonInfo(2))
          .Return(new Person { ID=2, Name = "A Stubbed Monica!" });

如何为扩展方法GetToken创建测试?

how can I create a test for the extension metod GetToken?

推荐答案

扩展方法只是静态方法的语法糖.因此,您真正需要的是能够在此处模拟静态方法的功能.不幸的是,这在Rhino Mocks中是不可能的.

Extension methods are just syntatic sugar for static methods. So what you really need is the ability to mock the static method here. Unfortunately this is not possible in Rhino Mocks.

有关更多详细信息,请参见以下StackOverflow线程

See the following StackOverflow thread for more details

要模拟静态方法,您需要一个模拟框架,该框架实际上使用CLR分析器来拦截方法调用.正如线程提到的那样,TypeMock应该能够做到这一点.

To mock a static method you need a mocking framework which actually uses the CLR profiler to intercept method calls. As the thread mentions TypeMock should be able to do this.

这篇关于如何在Rhino Mocks 3.6中将Expect设置为扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆