Firing Events Made Easy
I have already posted about Firing Events (There is a “Fire Event” party pic to the right). Here is how to use reflective mocks to Fire Events.
Here is the example again:
public void Example() { Button button = new Button(); button.Click += new EventHandler(button_Click); } private void button_Click(object sender, EventArgs e) { // Do Somethings here }
Now we want to test that the Example class works correctly when the button is clicked. Here is how we mimic the Button click, by mocking the Button.
Test] public void TestThatExampleWorksWhenButtonClicked() { Mock buttonMock = MockManager.Mock(typeof(Button)); // expect Example to add an event (and keep a hook to the event) MockedEvent clickButton =buttonMock.ExpectAddEvent("Click"); // invoke our test method that registers to button.Click Example(); // Fire the event clickButton.Fire(this, EventArgs.Empty); // here we test that Example works correctly //… MockManager.Verify(); }
Notice the clickButton.Fire used to fire the Click event.
Cool
Tip: The Event must be subscribed before using Fire. Guess why?
3 Comments to “Firing Events Made Easy”
Recent Posts
- Unacceptable: Unit testing will take 20 years to catch on
- The 4 reasons why we DIDN’T choose Oslo
- Typemock Academy Launch
- The First Rule to Software Craftsmanship
- Goal-driven Development
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- May 2010
- April 2010
- March 2010
- February 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- May 2008
- April 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006

[...] Using this and the Firing Events techniques we can simulate different button sequences. [...]
[...] It has been long enough since I’ve made my last post, but with the recent posting from Eli’s blog about TypeMock on Firing Events when mocking/testing, I thought I would throw out my solution to this problem for those of us using NMock2. [...]
[...] It has been long enough since I’ve made my last post, but with the recent posting from Eli’s blog about TypeMock on Firing Events when mocking/testing, I thought I would throw out my solution to this problem for those of us using NMock2. [...]