views:

608

answers:

4

How can I mock the database calls to make my application logic been tested without database?

A: 
procedure GetData (output arrayOfData)
  arrayOfData.record1.field1 = "dataA"
  arrayOfData.record1.field2 = "dataAB"
  arrayOfData.record2.field1 = "dataB"
  arrayOfData.record2.field2 = "dataBB"
  return arrayOfData)
end procedure

Then call GetData and use that chunck of data that you needed defined for your logic anyway. Later, change GetData to really get data from the database. Right now, just fake it, and assign it reasonable data by hand.

thursdaysgeek
+2  A: 

Use the repository pattern and mock it in your tests using a mocking framework such as MoQ.

Edit: check out this article by Stephen Walther on MoQ.

Mike Scott
I agree about using the repository pattern (and, hence, will up-vote). But, for as much as I like Moq, I don't mock my repositories with Moq. I think a hard-coded, mocked implementation, using the same interface as the "real" repository, is more flexible. But you're right that Repository is key.
Craig Stuntz
The article moved, new url is herehttp://stephenwalther.com/blog/archive/2008/06/12/tdd-introduction-to-moq.aspx
Allen
+1  A: 

Repository pattern with a hardcoded implementation or use an XML file (my preference).

Todd Smith
A: 

Hope this article by Stephen Walther helps..

This article walks the reader through the process of using mocks for unit testing. This is a detailed article and pretty good one.

It also has an example of the Repository pattern highlighted by some community members.

Mocking with Rhino Mocks (ASP.NET MVC)

rajesh pillai