Monday 23 June 2014

ViewModel Properties (3 of 3)

In part 3 I'm going to take what we've done with ViewModel Properties and show you how it dove tails very nicely into testing.

I use Specflow as my Gherkin tool of choice (you will need to install it if you want to play with the example). I won't go into detail about the philosophy of BDD and Gherkin. If you want to do some reading you can go here and here. But in short I've used it on several projects and I'm starting to think it's a pre-requisite for any large project.

Before we go any further here is a peek at one of the Gherkin tests. You'll see they are very simple and easy to read (even a BA with no coding ability would be able to understand it). For each Given/When/Then line there is a method in code that handles the step. It all takes a bit of getting used to but it's very much worth the effort!


Let's take a look at why our ViewModel Property helps us with testing. If you recall, we have a ViewModel Property Collection within our ViewModel to help us keep track of and manipulate our Properties as a whole. What this also gives us is a method called GetSnapshot. This method will return a Dictionary (key/value) with all the relevant data for a Property (it just iterates over all the ViewModel Properties and calls GetSnapshot on them). With this data we have a snapshot of the state of our ViewModel.


From there setting up the tests becomes relatively trivial. You create the tests using actions applied to your ViewModel and expected states of your ViewModel as validation (that's all testing is really, perform an action and validate the state of the object we're testing). If you're using BDD you could create Acceptance Tests off the back of your Specifications. If you're using TDD, you would create all the test scenarios before you've written any code.


Obviously there is a lot I haven't discussed here. The best way to learn about something is to get your hands on it and dive right in. So please, if you are interested in this approach, go to the GitHub repository, and have a play. And of course, any feedback would be appreciated!

No comments:

Post a Comment