It is actually pretty easy to raise events using the latest release of NMock. You can basically use Expect.Once.On(bla).EventAdd(“SomeEvent”, Is.Anything) to specify that you expect an event handler to be added for “SomeEvent” event on bla object and fire that event using Fire.Event(“SomeEvent”). Here’s an example, minus any domain noise, that demonstrates the whole thing.
What will happen when the following piece of code is compiled? Options: Compiler error. “Moved” will be printed.
If you have an application that deals with payment processing, social-security numbers, etc., there’s a possibility that such sensitive information can be written to log by a component in the application. While you need to take appropriate measures to ensure that the log files are sufficiently protected, you may also want to prevent such information… [Read more…]
Consider the following interface that takes a variable number of arguments in its Add method. And consider that we have to test the following function which invokes Add with two arguments. The expectation created below is incorrect: It will cause NMock to generate the following exception because NMock is expecting two distinct arguments to Add… [Read more…]
The best thing about fluent-style APIs is that they are readable and flow easily. Consider the following for example: The API used above is clearly more readable than: Another reason I like them is that fluent style APIs are easier to pick up. You can rely on intellisense to guide you more as you have… [Read more…]
Singleton is probably one of the most (mis-)used design patterns. In this article I intend to lay out a scenario and then demonstrate how we can go about testing methods that depend upon a singleton class. Finally I’ll recap the whole approach in a bulleted list for easy reference. Let’s restrict our problem to involve… [Read more…]
In an ideal world one would write unit-tests before writing any new code following the tenants of TDD. In reality it is quite common to have to rely on a legacy library to do some heavy lifting. Pex is a great new tool available from Microsoft Research that can be used to generate unit-tests automatically.… [Read more…]
Indexers allow objects to behave like arrays. Dictionary class in .NET, for example, provides an indexer that allows the retrieval of the item from the dictionary that corresponds to a particular key. There are two ways you can go about mocking indexers defined on interfaces when using NMock. You can use the Get property to… [Read more…]
There appears to be inadequate documentation on setting up expectations in NMock for methods that accept arguments by reference using “out” and “ref” keywords. Here is a quick note to give an example demonstrating how it can be done. Consider the following interface: “Bar” method takes one “out” parameter and another “ref” parameter. To start… [Read more…]
If you have been blessed with a windows-forms application that has business-logic invading the user-interface, I have something that’ll help you. The most practical approach in this situation is to rearchitect the business layer and switch the UI over in a piecemeal fashion. While you’re doing this, automated testing of the user-interface will make your… [Read more…]
March 4, 2009
0