How can I mock the database calls to make my application logic been tested without database?
3 Answers
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.
-
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. Dec 11, 2008 at 14:02
-
The article moved, new url is here stephenwalther.com/blog/archive/2008/06/12/… Apr 30, 2009 at 19:21
Repository pattern with a hardcoded implementation or use an XML file (my preference).
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.