BDD with MSpec

Behavior-driven development (BDD) is a process of developing the software that has evolved from the TDD (test-driven development). We can think of a BDD such as the feature driven approach to the TDD. The main advantage of the BDD over the TDD is the way the tests are being written, in a form of the story that can be understood both by the developers and the customers. In short, we can simply state that the BDD is the TDD done right – it makes use of the so called ubiquitous language which is the core part of the domain and the concepts presented by the customers (domain experts). In this text, I’ll present how you can start using the BDD approach in your code and the general idea behind that. We will start with our simple “framework” and then proceed to the MSpec (yet there are other great libraries such as the SpecsFor or NSpec and even more of them). In the examples below, I’ll be using the NUnit as the testing framework and the Moq as the mocking framework. As for the tests itself, they will be the real live examples from my open source project called Sentry.


Let’s start with our own approach to the BDD and see where it leads us. It’s rather simple, but powerful enough for the most cases. BTW kudos to Marcin, who actually influenced me to focus on the BDD quite some time ago.

And this would be our tests (please note that I’m doing a lot of the async work and also the async tests, which can be easily fixed by changing the methods in the SpecificationBase class to return the Task instead of the void):

My guess is that you don’t even need to know what really the library that I’m working on is about (however I do encourage you to find out), yet you should be able to realize what it does just by looking at the names of the specification classes and testing methods. It’s like the story with its acceptance criterias – and that’s true, since BDD is all about identifying the behavior. Honestly, you can run such tests that are written that way and show your customer or the project owner that they all pass (hopefully) and he should be able to understand what’s going on, just by reading them out loud (or quietly). And that’s what’s all the fuss about. Once you get the general idea, let’s take a look at how we can simplify these tests with the usage of the MSpec:

It’s kind of a beauty, isn’t it? As long as you don’t really care about having the static properties in your tests go for the MSpec (these statics are a must, due to the usage of the delegates). Otherwise, you can use some other libraries (e.g. SpecsFor, which has also a great integration with the MVC) or stick to the custom approach (probably improved), which has been presented at the beginning of this post.

3 Comments BDD with MSpec

  1. Pingback:

  2. Pingback: It’s been over a year | Piotr Gankiewicz

  3. Pingback: Becoming a software developer – episode VII | Piotr Gankiewicz

Leave A Comment

Your email address will not be published. Required fields are marked *