Hi Chris,
I'd like to break out the expectation setting (with event subscriptions) in one place, then test EventC here without testing the other events. Can I do this?
Yes you can.
However, this will be easier using Reflective Mocks, mainly because Reflective is not strongly typed and uses strings to set expectations.
(It can be done in Natural but you'll have to resort to using reflection which is more complex)
Look at the following example:
[Test]
[VerifyMocks]
public void EventTestReflection()
{
MockObject mockView;
MockedEvent handleEvent = MockEventFiring(out mockView, "EventA", "AFired");
IView view = mockView.Object as IView;
MyClassUnderTest target = new MyClassUnderTest(view);
target.SetupEvent();
handleEvent.Fire(null, null);
}
[Test]
[VerifyMocks]
public void EventTestReflectionB()
{
MockObject mockView;
MockedEvent handleEvent = MockEventFiring(out mockView, "EventB", "BFired");
IView view = mockView.Object as IView;
MyClassUnderTest target = new MyClassUnderTest(view);
target.SetupEvent();
handleEvent.Fire(null, null);
}
private static MockedEvent MockEventFiring(out MockObject mockView,string EventName,string EventHandler)
{
Mock mockTested = MockManager.Mock<MyClassUnderTest>(Constructor.NotMocked);
mockTested.ExpectCall(EventHandler).Args(null, null);
mockView = MockManager.MockObject(typeof(IView));
mockView.Strict = false;
return mockView.ExpectAddEvent(EventName);
}
:!: Pay attention that I've set the view strictness mode to false. This is necessary since i assume that your tested class registers on all events which means that there will be several unexpected calls Add_Event in each test.
I hope this example is in the spirit of what you needs
let me know if you need any clarifications or if this is not what you've needed