There has been a lot of discussion comparing anonymous delegates to closures and blocks in Ruby, so I’m not going to get into that whole thing…
What I want to talk about for a minute is how cool it is to use anonymous delegates to test that events on your classes fire as expected.
Suppose, for example, that you have a class like this:
1 2 3 4 5 6 7 8 9 10 11
Now before anonymous delegates, you would have had to introduce a private method in your test fixture to attach to the ItemAdded event. And as shown here, you probably would have had to introduce a private field to pass data between the event handler method and the test method. (Sure, you could have created a mock class to hold the state and the event handler, but that would have been even more code to read/parse/grok.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
With anonymous delegates, you can now write very clear, very clean tests like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
There are a couple of cool things in this code:
- The delegate can modify variables in the test method, outside the event handler itself
- The entire test’s meaning is clearly stated within the test–you don’t have to look in some other method or class to see what’s happening
- Because the entire test is self encapsulated, it is easier to move/copy/change should you need to do any refactoring to either the class under test or the test fixture itself.