How to set Expect to a Extension method in Rhino Mocks 3.6

Deadly 提交于 2019-12-11 08:15:56

问题


Good afternoon

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

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

Extension method:

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;
   }
}

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

}

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

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

and the main Expect is

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

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


回答1:


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.

See the following StackOverflow thread for more details

  • Mocking Static methods using Rhino.Mocks

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.




回答2:


It should be possible by creating Stub for the extension methods.

Extension methods in Mono 2.4 and RhinoMocks 3.5



来源:https://stackoverflow.com/questions/5488659/how-to-set-expect-to-a-extension-method-in-rhino-mocks-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!