Natural Mocks becoming more intelligent

Natural Mocks automatically mocks everything. This includes return values. So the following code:

using (RecordedExpecations recorder = RecorderManager.StartRecording()) { DummyClass.DoWork(); recorder.Return(new SqlInt32(1)); }

Won’t work as expected because SqlInt32 is being mocked.

This has bugged me a few times and I simply created the return value outside the recorder.

SqlInt32 mockValue = new SqlInt32(1)); using (RecordedExpecations recorder = RecorderManager.StartRecording()) { DummyClass.DoWork(); recorder.Return(mockValue); }

But this is not really nice, and a few customers have pointed out that this could be annoying!

So we are going to implement a feature that will know that you are creating a return value and will not mock it!
This way the first code will work as expected. Of course if you want to return a mocked value you will have to create the mock before, this might lead to non backward compatibility.

To those who want the first code to work as before (although I don’t think that anyone would), mock the return value before calling the recorded statements, and change the above code to:

using (RecordedExpecations recorder = RecorderManager.StartRecording()) { SqlInt32 mock = new SqlInt32(1); DummyClass.DoWork(); recorder.Return(mock); }

Of course if you are going to do that, you will have to define the behavior of the SqlInt32 and you might as well use ChainedMocks

using (RecordedExpecations recorder = RecorderManager.StartRecording()) { DummyClass.DoWork().Value; recorder.Return(1); }

What do you think about this feature?

One comment

Add Comment

Required fields are marked *. Your email address will not be published.