How to pass a method as a parameter without a delegate declaration in .NET.

No matter how I try, I cannot mimic the pure Rhino Mocks syntax without declaring a delegate.

Example:

Expect.Call(service.HelloWorld("Thanks"))

Do you have any ideas on how to do this?

Thanks.

+3
source share
3 answers

You can use the Action delegate provided in new versions of .NET.

void Execute(Action action) {
    action();
}

void Test() {
    Execute(() => Console.WriteLine("Hello World!"));
}
+7
source

Using Lambda syntax in 3.5, you can get a similar syntax.

public void Call(Action action)
{
    action();
}

Expect.Call(() => service.HelloWorld("Thanks"));

Moq is a mocking structure that uses lambda syntax to mock him.

var mock = new Mock<IService>();
mock.Setup(service => service.HelloWorld("Thanks")).Returns(42);
+6
source

Rhino Mocks . , , . . , .

0
source

Source: https://habr.com/ru/post/1708228/


All Articles