2

I have a dependency that gets called when the object I'm testing is created. However, it should never be called after that. How would I write such a test?

I'd like just this line as my test (since I'm trying to follow the AAA style of test writing). However, this assertion will fail since the Publish method was called during setup.

Notifier.AssertWasNotCalled(Sub(n) n.Publish(Arg(Of Message).Is.Anything))

Is there a way to "reset" the calls on the dependency I've mocked?

Note: I can set it up so that I check the property on the Message argument for a value I expect after initialization, but that makes my test more fragile/brittle and I'd like to avoid it if possible.

_notifier.AssertWasNotCalled(
  Sub(n) n.Publish(Arg(Of Message).Matches(Function(m) m.property = "yo!")))
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Jeff B
  • 8,572
  • 17
  • 61
  • 140

1 Answers1

2

Here is how I would do it:

_notifier.AssertWasCalled(function(n) p.Publish, function(c) c.Repeat.Once().IgnoreArguments());

As this would make sure it is called once only, which would be triggered by your setup code as you have indicated.

Pondidum
  • 11,457
  • 8
  • 50
  • 69
  • 1
    That works great! In my case, the method is called multiple times (I'm doing a `for each` over a list returned by a stubbed method)... but since I can just do `Repeat.Times(stubbedList.count)` is works fine (with no magic numbers). Yay! – Jeff B Jan 27 '12 at 21:53