2/24/2023 0 Comments Rhyno mocks![]() ![]() First we set up a stub for when the AMethodThatRaisesAnEvent() is called. It may look a little scary but if you break it down into it’s components, it’s actually prettty easy to grok. In the first few lines we arrange the objects we want for the test, then we get into the juicy bit. Processor.Raise(x => x.AnEvent += null, processor, args) Processor.Stub(p => p.AMethodThatRaisesAnEvent(0)).IgnoreArguments() Var listener = new EventListener(processor) Var processor = MockRepository.GenerateStub() Public void TheEventListenerShouldCauseProcessingToBeCancelled() It’s a silly thing to do in reality since normally you would check property values on the sender and decide wether to cancel or not, but for the purposes of the post, it will do. When it fires it checks the senders type and and sets the Cancel flag. }So as we can see, the constructor takes in the processor object and starts listening for the AboutToProcess event. Public void HandleTheEvent(object sender, CancelEventArgs args) Return processor.AMethodThatRaisesAnEvent(0) ![]() Public EventListener(IProcessor processor) String AMethodThatRaisesAnEvent(int value) I like to test behaviour in my classes, so in my test I want to be sure that my class under test correctly subscribes to the event and that it will set the cancel flag correctly. Let’s start with a basic scenario… let’s assume that in our class under test we wanting to check that when we call a method on another object, that it will raise a CancelEvent and we can listen to that event and take action as appropriate. I was just helping out someone today and showing them how to do it using the Arrange, Act, Assert (AAA) syntax in Rhino Mocks 3.5 and I thought you might be interested in know how to do this as well. There’s various posts on the web showing how to raise events in Rhino Mocks but they typically show you how to do it using the Record/Replay syntax, which I personally find quite awkward. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |