Any way to reset a mock?

3,754 views
Skip to first unread message

samuel_d_jack

unread,
Sep 25, 2009, 6:08:32 AM9/25/09
to Moq Discussions
Is there any way to reset a mock, so that even though methods might
have been invoked on it, after the reset, a call to mock.Verify(...,
Times.Never()) succeeds?

My situation is a test where I want to ensure that my component isn't
doing more work than necessary. So setting the property the first time
should update the component, but setting it again to the same value
should do nothing. Obviously in my test I will need to call the
property the first time, and this will cause methods to be invoked on
the mocks. Then I would like to reset the mocks before setting the
property for the second time and verifying that the mocks are not
called again.

Any ideas? Would this be a good feature request?

Thanks,

Sam

Daniel Cazzulino

unread,
Sep 27, 2009, 12:08:36 PM9/27/09
to moq...@googlegroups.com
Why wouldn't you just Verify with a different Times? (i.e. just Times.Once()) on the calls to the mock?

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471

samuel_d_jack

unread,
Sep 30, 2009, 4:40:03 AM9/30/09
to Moq Discussions
That makes the test more dependent on implementation details: you need
to calculate exactly how many times you expect the method to be called
in the setup stage and then add one (or whatever) to it for the times
you expect it to be called at the stage you're actually testing. It
also makes the intention of the test less clear: somebody reading the
test wouldn't immediately know why you're expecting the method to be
called n times, rather than once or never.

On Sep 27, 5:08 pm, Daniel Cazzulino <k...@clariusconsulting.net>
wrote:

Daniel Cazzulino

unread,
Sep 30, 2009, 5:59:03 AM9/30/09
to moq...@googlegroups.com
Times is a verify stage thing, not a setup one.

I fail to see how it obscures intent...


/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


Reply all
Reply to author
Forward
0 new messages