tags:

views:

89

answers:

2

Is there some .Net mocking framework that allows to capture the actual parameter passed to method to examine it latter?

Desired code:

var foo = Mock<Foo>();
var service = Mock<IService>();
service.Expect(s => s.Create(foo));
service.Create(new Foo { Currency = "USD" });
Assert(foo.Object.Currency == "USD");

Or a bit more complex example:

Foo foo = new Foo { Title = "...", Description = "..." };
var bar = Mock.NewHook<Bar>();
var service = new Mock<IService>();
service.Expect(s => s.Create(bar));

new Controller(service.Object).Create(foo);

Assert(foo.Title == bar.Object.Title);
Assert(foo.Description == bar.Object.Description);

The idea is to get passed parameter. And then execute aseertions agains it.

A: 

If you need to assert on the parameter passed to an object, it seems you're subjecting the wrong object to your test. Instead of asserting the parameters passed to a method, write a test for the calling class that asserts the correct parameters are passed.

Tomas Lycken
I try to test that Controller.Create will create and pass correct object into IService.Create.
alex2k8
+2  A: 

I think you're looking for something equivalent to Moq's Callback:

var foo = Mock<Foo>();
var service = Mock<IService>();
service.Setup(s => s.Create(foo.Object)).Callback((T foo) => Assert.AreEqual("USD", foo.Currency))
service.Object.Create(new Foo { Currency = "USD" });
Blake Pettersson
Yes, this would work. Thank you!
alex2k8
I really like Moq. http://code.google.com/p/moq/
TrueWill