1

I am using the Dapper QueryMultipleAsync to split the result of a SQL query into a number of different objects.

However I am having trouble finding a way to unit test the method that this call is in. I have been using Fake it Easy to fake out elements for the unnit tests. As part of the tests I wanted to fake the QueryMultipleAsync to check other calls are made, however which ever way and at whatever level I try this I seam to get errors.

Does anyone have any experience trying to fake out this dapper element? If so how did you do it?

Simon.A
  • 11
  • 3
  • 4
    I *guess* you could fake the connection, but... that's getting into some awkward areas - there is a *lot* of stuff that needs to be implemented for connections to work; frankly, I'd probably advise encapsulating the data access code in a separate piece, and *fake that as an entire unit*, i.e. so you have a faked `var orders = await dal.GetOrdersAsync(customerId);` (or whatever), where `dal` is the thing being faked; the fact that it uses `QueryMultipleAsync` internally being an implementation detail, to be checked via *integration tests* – Marc Gravell Jan 17 '18 at 10:18

1 Answers1

1

I agree with @Marc Gravell's comment, in that I'd encapsulate all the data access code and test it via integration tests. One other point is that QueryMultipleAsync is not a virtual method, so it cannot be faked by FakeItEasy; adding a fakeable layer of abstraction is the only way to isolate that call from the code you want to test.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111