locked
How to unit test repositories containing complex queries? RRS feed

  • Question

  • User1058130854 posted

    I have a question, if we push complex/multi table queries into the repository layer which I agree is where it should belong - how would you go about unit testing this logic without having a database?

    I might add, that normally I would have that when unit testing, you would implement a dummy IRepository, or mock the IRepository so that it would return your domain objects, and you would then unit test the controller classes with a mocked repository.

    However by pushing complex queries into the repository itself, it makes it difficult to mock - I mean you wouldn't want to reimplement your complex query logic in the mock, which really only leaves testing with a real database connected, which makes it an integration test

    Dave

    Wednesday, May 15, 2013 7:33 PM

Answers

  • User-837620913 posted

    Correct to your last statement that this is an integration test. When reading your question (before I read the body) I said to myself "just use an integration test." If you have setup things correctly, you should be able to swap out a LocalDb implementation in your test project's App.config and attach a database at test run time so you will have known values for the query (see this link:  http://stackoverflow.com/questions/12244495/how-to-set-up-localdb-for-unit-tests-in-visual-studio-2012-and-entity-framework).

    Everything doesn't have to be unit tested - especially if you have an integration test covering it. Heck there are parts of your application that you may never test (e.g., the visual layout on pages).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 17, 2013 6:01 AM

All replies

  • User-837620913 posted

    Correct to your last statement that this is an integration test. When reading your question (before I read the body) I said to myself "just use an integration test." If you have setup things correctly, you should be able to swap out a LocalDb implementation in your test project's App.config and attach a database at test run time so you will have known values for the query (see this link:  http://stackoverflow.com/questions/12244495/how-to-set-up-localdb-for-unit-tests-in-visual-studio-2012-and-entity-framework).

    Everything doesn't have to be unit tested - especially if you have an integration test covering it. Heck there are parts of your application that you may never test (e.g., the visual layout on pages).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 17, 2013 6:01 AM
  • User1058130854 posted

    Thanks Darrell, thanks for the link too good stuff

    Sunday, May 19, 2013 11:53 PM