views:

240

answers:

2

I've just started out using ASP.NET MVC and TDD. I've read that while unit testing you should focus on testing your code, not other systems, like the DB. However, what happens when essential functionality is residing in the DB?

I've used the MVC Storefront series as an initial guide in how to set up my projects and patterns. As full text search is essential I've set this up as a function in my repository

IQueryable<HealthOrganization> SearchOrganizations(string phrase);

Now, the logic of this search needs a table valued function with full text and some other functions in the database. The logic in these functions is impossible to get exactly correct in the fakerepository. What is your recommendation on the best strategy to unit test this and to do TDD on the search?

A: 

This is not a unit test, it is an integration test. So you hardly can implement it TDD like.

I assume that your code doesn't do much but calling the database. So you don't have to test a lot. If you can mock the database call at the lowest level, you can test if the call happens. If this gets very complicated, it's better to not write a unit test for it. It will only be hard to maintain.

To write the integration test you need a real database.

Stefan Steinegger
That's sort of what I figured. But, if you have an application where search is an important feature, wouldn't you want to develop the search Test first to Test drive it? Could you start with the integration test?
Tobias Rundbom
@rundbom: You can, I just wouldn't do it. Writing integration tests is time consuming. With TDD you want to be quick. If not, the "flow" gets interrupted. And there is another reason. With TDD you want to achieve that _your_ code is doing what you attempt to do. It is not testing if the whole system works as expected. It's hard to explain, but when an integration test fails it has another meaning as when a unit test fails.
Stefan Steinegger
A: 

You could create a test database containing a set of known items, then you check the search code returns the results you expect..

dbr