4

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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
The article moved, new url is here stephenwalther.com/blog/archive/2008/06/12/…
1

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

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.