Most of us use some sort of a mocking library when doing unit tests in order to mock our service and repository layers right!
Well I am a big fan of Rhino Mock and I have used it in almost all of my testing projects.
But one thing that remains to be a pain in the ass is creating test objects to be returned when the mocked repository is called. Well not any more!!!
Using NBulder we can avoid hard coding test objects for mocking purposes.
Here are some sample codes to get you started with
Let's say you want to mock a a repository call that returns an item, just write
var item = Builder<RepositoyType>
//set the id property of each items (Nhibernate is used with Windsor Castle in this occasion)
EntityIdSetter.SetIdOf(item, 1);
To mock a method call that returns a list of record objects.
var items = Builder<RepositoyType>
for (int i = 1; i < items.Count + 1; i++)
{
EntityIdSetter.SetIdOf(items[i - 1], i);
}
As you can see from the above example we can even set specific values for the attributes for returned objects as well.
And there are many more helper functions you can use to accomplish almost all the test object creation needs you hope for.
Happy Coding!!
Welcome
"Happy are those who dream dreams and are ready to pay the price to make them come true"
About Me
My Home @ google Map
- Dhanushka Athukorala
- Colombo, Western Province, Sri Lanka
- I'm a Microsoft Certified Professional (MCP-ASP.net) who is currently working as a Software Engineer @ Aeturnum. I have experience in windows, web, mobile application development using .net framework 2.0, 3.5, 4.0 and .net compact framework 2.0 and 3.5 and i have also carried out development projects in C++, C, VB.net, Java, PHP, Pl/SQL, SQL, XNA, AJAX, Jquery etc...
Followers
Posted by
Dhanushka Athukorala
Tuesday, December 21, 2010
Subscribe to:
Post Comments (Atom)