Archive for May, 2007

Some TDD concepts

May 15, 2007 2 comments

Note: The tips below are for the NMock framework, but the concept might be same for other mocking frameworks. Please refer the complete article (from where I’ve taken these tips).

Traditionally, unit testing terminology has included the concepts of drivers and stubs.

A driver is a piece of software that is written with the sole purpose of accessing properties and methods on a library to test the functionality of that library. A stub is a piece of software whose only purpose is to provide the library under test with an implementation of any modules it may need to communicate with to perform its work.


As illustrated in the figure, this concept translates well into practical terms. With the tools available today, you have a framework for implementing both drivers and stubs in a dynamic and flexible way. NUnit is an implementation of a driver and for stubs NMock can be used to create dynamic mock objects at run time.

The default behavior of a mock instance is to return null from every method call that returns a reference type and to return the default value from every method call that returns a value type. When dealing with methods with return values, the default behavior of the mock instance isn’t sufficient anymore. It is necessary to instruct the mock object about what to do.

Setting Expectations
The SetupResult method is still a rather blunt instrument. Any call to the configured method is answered with the same return value, no matter what parameters are used in the method call. To configure the mock instance to return appropriate values for different parameter values, the ExpectAndReturn method is used.

Consider a test where you use SetupResult. Aside from the obvious disadvantage that all calls to the same method return the same value, you don’t get much validation out of the mock object. Regardless of whether your target library called a method zero, once, or many times, you wouldn’t notice the difference. Conversely, you can use ExpectAndReturn to validate that a method was called the expected number of times, with the expected parameters, and in the expected order. With this approach, a unit test not only has the opportunity to test that a module returns the correct values, but also that it communicates with any external dependencies as designed. In fact, with some modules where most of the implementation is internal, this may very well be the only way you can perform tests in any meaningful way.

If we have no way of knowing the value of the some parameter in advance, how can we define the expectation? The answer lies in the NMock Constraint classes (the IsEqual constraint is the default).

Calling the Verify method at the end of your unit test tells the mock object that the test is over and that it should verify that all members were accessed the expected number of times. If a member happened to be accessed fewer times than expected, a VerifyException would be thrown.

To summarize the steps thus far, you set the Strict option to true at the beginning, stick with the expectation-setting methods throughout, and call the Verify method at the end of the unit test. With very little effort, you’ll have added a whole new level of validation to your unit tests.

In real-world software development projects, it is a rare library that doesn’t have some kind of external dependency with which it communicates frequently. When unit testing libraries, you need some kind of stub to simulate those external dependencies. NMock provides you with a valuable tool for achieving this with minimal effort. Although writing unit tests still involves work, NMock offers relief from writing, configuring, and deploying your own stub classes over and over again. Defining mock instances and their behaviors at run time also gives you a degree of flexibility and test validation you probably never would have been able to achieve if you wrote your own stubs for each external dependency.

Categories: Programming, Unit Testing

Hello world!

May 11, 2007 1 comment

Welcome to This is your first post. Edit or delete it and start blogging!

Programmers will be programmers 🙂 Thank you all wonderful WordPress guys who have made such a beautiful blogging site. That’s the reason, I am not deleting the above ‘welcome to wordpress’ sentence 😉

For the benefit of people who are not into software, let me tell you the significance of ‘Hello world!’. The first program which I (and many of those who started learning programming with C or Java) wrote was ‘Hello world!’. This simplest of the programs, ‘Hello world!’ is so popular that it has become a de facto first program in most of the books that teach programming in any language.

So here I am with my blog, and aptly, the first post is titled ‘Hello world!’ (do the non programming guys agree?). I definitely read a lot of blogs by other smarter guys (though mostly programming related blogs) and thanks to all of them for sharing their knowledge. Today, I am no expert to share my knowledge with the world, but I hope to become one soon.

However, I can definitely share what I like from the various books, articles or blogs I read. In fact, a blog could become a nice repository of information. Previously, if I liked some article from some newspaper, I used to cut it and keep a hard copy with me, which easily got damaged / lost over a period of time. And then this brilliant idea stuck me – to keep that data in soft form (e-form) on my blog, so that at any time in future, I can find it without much pain. So here you’ll find information / excerpts from the various books on programming, philosophy which I found thought-provoking, or sometimes, full articles / poems from different sources.

I must also thank my cousin, Bhushan, for inspiring me to start my blog 🙂 because I dreamt of having my own website, but which has not happened till date. And this blog is quite a close substitute, with quite less effort and much ease. For any of you who are interested in drawing, photography or animation (2D – 3D), have a look at his nice blog.

So happy blogging (to all bloggers, including me) and enjoy reading.

Categories: Uncategorized