I'm going to following the principles of TDD as I understand them:
- Write the test first - it'll probably not compile.
- Get the test to compile.
- Get the test to pass - with the least amount of code.
- Create another test that will cause your code to fail.
- Refactor your class so that both tests pass.
As I've been refactoring my code I've come across the follow functionality...
private bool DoesMonthYearExis( int month, int year ) { bool exists = false; var months = ( from m in budgetEntities.MthSet where m.Month == month && m.Year == year select m ).ToList(); if ( months.Count > 0 ) { exists = true; } return exists; }
There will obviously be a number of steps involved in turning this into a WCF Service call, but the first step is to ensure that the DataService will do what it's suppose to do. So how do we do this using a TDD approach? Firstly we write our test.
So here we have our first test using the new Moq framework. As you can see the data service interface doesn't have a definition for a method called DoesMonthYearExist(...) hence it won't compile, which leads to...
Getting the test to compile
To get this to compile we need to update the Interface as well as it's implementing class. We need to remember that we write the least amount of code as possible.
Get the test to pass
Don't forget to write as little code as possible.
You can see that the expectations will now be met with repo.Query() returning a value and the repo object being disposed.
This is all good, but we know that the code is not really doing what we need it to do and this is when we...
Create another test that will cause your code to fail
Not a huge difference, but it's enough.
We've changed the year being tested for and are now expecting that the combination of month and year will not be found.
Refactor your class so that both tests pass
Here we make our implementation do what it's supposed to do.
What does this leave us with? A well tested method that has good coverage
Moq
I must say that I do like not having to indicate what expectations have to be recorded and then play them back, you just say this object has certain expectations and that's it.
Despite simple examples Moq looks like a nice mocking framework to work with.
Enjoy!
No comments:
Post a Comment