Latest news for free sample offers in canada
UPDATE: I finally am posting the source !
It has been long enough since I've made my last post, but with the recent posting from about TypeMock on when mocking/testing, I thought I would throw out my solution to this problem for those of us using .
If you're not familiar with mock objects, there is some great content out there, just google it (you didn't think I was actually going to explain them here did you?!)
First off free sample offers in canada, I love NMock2. It does what I need and stays out of my way. The syntax is terse and flows like prose.
On with the content!
Part 1: Ready
It seems obvious to me, that if you're using mock objects, you're most likely going to run across a mock that needs to fire an event. It's almost like the NMock2 guys completely skipped over this use case (but I'm not knocking them, [free sample offers in canada] this is a great product). After digging around in reflection trying to figure how to do what I wanted and discussing it with a colleague of mine, I was turned on to the idea of using Custom Actions to catch the event add's and remove's [part 1] and store them off for invocation later (when the event is actually fired) [part 2].
Part 2: Aim - Catching the Event Add's and Remove's
The great thing about a custom action (type IAction), is that you get all sorts of information about the thing that actually got invoked. In our case, the adder or remover of an event. Similar to properties, the add/remove 'accessors' are compiled into methods just like the get/set accessors. The naming of these generated methods is also similar. For a property's get/set, the names look like:
Likewise, for the add/remove 'accessors' for an event get names:
Armed with this information, we can tell if the method being called is an event adder/remover. The syntax for catching an event add is as such:
Similarly, event removal looks like:
Part 3: Fire!
Great! We know when events are added! But wait, it still seems like we haven't gotten anywhere (but really we have). The Catch. EventAdd takes the method that got called (add_Click), and grabs the single argument from the method call which happens to be an EventHandler type delegate (in this case), and stores it off to be invoked later when the event gets fired.
This call invokes all of the methods that were caught when being added to the mockButton's Click event with the given parameter values.
Often times it is useful for an event to be fired when some other mocked method gets called. For this case, a custom action should suit just fine; simply pass it into the Will portion of your expectation/stub setup for that particular method. That means, as soon as the mocked method gets called, the event will fire just as if the above line of code was executed.
Here is a complete example that pulls it all together:
Thats it. This stuff is currently unreleased, but feel free to send requests, I'll gladly send a copy.