Where was the stubbing part in Moq?
In my very recent previous post I said “mocking and stubbing easier than ever”, but actually forgot to mention the stubbing part :S.
This one is not new for users of moq-contrib, but we decided to move this to the core Moq library as we get the question on how to stub properties often enough… :)
It’s actually rather simple and easy to implement just by using Moq external API, but as a facility, here’s what you can do:
var mock = new Mock<IHaveValue>();
// start "tracking" sets/gets to this property
mock.Stub(v => v.Value);
// alternatively, provide a default value for the stubbed property
mock.Stub(v => v.Value, 5);
// Now you can do:
IHaveValue hv = mock.Object;
// Initial value was stored
Assert.Equal(5, hv.Value);
// New value set which changes the initial value
hv.Value = 6;
Assert.Equal(6, hv.Value);
What is new in this version beyond the original moq-contrib stubbing, is the ability to stub all properties of the object in a single call:
var mock = new Mock<IFoo>();
mock.StubAll();
This feature integrates seamlessly with the default value behavior specified for the mock, as explained in the previous post, meaning that you can stub all properties and cause them to return new mocks when appropriate, also recursively (but lazily!), in a single call. This may be redundant to note, but it’s just to point that the API and behavior is still consistent.
If you want to have some fun reading rather crazy reflection API usage to invoke various generic methods including ones receiving Func
Next: out/ref parameters in Moq!!!
/kzu
/kzu dev↻d