3

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

Example:

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

Do you have any idea on how to do this?

Thanks.

3 Answers 3

7

You could use the Action delegate provided in newer versions of .NET

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

void Test() {
    Execute(() => Console.WriteLine("Hello World!"));
}
Sign up to request clarification or add additional context in comments.

Comments

6

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 framework that uses Lambda syntax for it's mocking.

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

Comments

0

In Rhino Mocks it is actually invoking the method. The object is in setup mode at that time and when you invoke it, it is recording parameters and setting expectations. This is why you can get away from the delegate syntax. Not really possible in many other scenarios unfortunately.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.