Как установить ожидаемый метод расширения в носорогах 3,6
-
14-11-2019 - |
Вопрос
Добрый день
У меня есть класс, и он имеет связанный метод расширения.
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;
}
}
.
Теперь я пытаюсь использовать носорог и тестировать этот метод
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 ()
var _interface = MockRepository.GenerateMock<IMyInterface>();
.
и основное ожидание составляет
_interface.Expect(x => x.GetPersonInfo(2))
.Return(new Person { ID=2, Name = "A Stubbed Monica!" });
.
Как я могу создать тест для расширения Metod GetToken?
Решение
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
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.
Другие советы
It should be possible by creating Stub for the extension methods.