Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top