Rhino Mocks EventsPurity vs. Practicality
One of the core principals of Rhino Mocks is "No Strings", and I have gone to great lengths in order to avoid using strings in Rhino Mocks. However, at one point you must stop and consider if this is really worth it. I believe that I have reached into the limits of what the language can do.
Here is how you can raise an event in Rhino Mocks today:
mockedView.Load += null;
IEventRaiser loadRaiser = LastCall.IgnoreArguments().GetEventRaiser();
No strings involved, and it is safe to use in the face of refactorying. However, it does create an expectatin that something will register to this event, but that is something that you want to happen anyway. Today I read this piece of code:
I consider this a hack, because this is working around the way Rhino Mocks is supposed to work. Nevertheless, this is a valid use case if you don't want/care to know about event registration. In the case of a stub, you truly want to only get the event raiser, without the line noise of the version above.
So, in order to support this scenario without all the casting, you can now do this:
Just to make this clear, I don't really like this syntax, and I would recommend using the one above, but it is a valid request.
You can get it from the repository, or wait a few days until I make a release.
More posts in "Rhino Mocks Events" series:
- (09 Jun 2007) Twisting the Syntax
- (08 Jun 2007) Purity vs. Practicality
Comments
I sent you an email about this about something like this a year ago... I don't remember if you responded, but basically I suggested syntax like this:
obj.Bar += mocks.Raise<Foo>(EventArgs.Empty);
and behind the scenes it would look kind of like this:
// Obj class:
// MockRepository:
So basically you'd set the flag and then ignore the next call to an event but actually raise the event instead. Kind of hacky, but it gets rid of strings.
Aaron,
Nice syntax, but it is very un-natural thing to do.
Still, something to remember for sure.
i downloaded the latest version of Rhino mock. but could find the the interface IEventRaiser.
what could be the probelm.
i shifted from NMock because there is no event support for nmock.
What version did you get? 3.1?
It should definitely be there
Comment preview