Search code examples
c#.netunit-testingmockingsqlkata

How could I unit test a function which uses SqlKata?


How could I unit test a function which uses SqlKata?

I am using the QueryFactory to create and execute queries and as I can see there is neither interface for it, nor any of the methods are virtual. Meaning, I can not mock the results and behavior of the QueryFactory, Query and the the methods like Where, Select, WhereIn, etc.

So, if I have a method which uses SqlKata I have no idea how to mock the SqlKata dependencies. Ergo, I can not test it. Hope someone knows how to mock the SqlKata and could share that knowledge, hence the question.

May there be any reasons that low level ORMs are not supposed to be mocked? And as a result the SqlKata developers did not put interfaces/virtual members in the library.


Solution

  • i have just wrote my own this code (using xUnit):

    var mockQF = new Mock<QueryFactory>();
    string p1 = "my condition value";
    mockQF.Setup(_db => _db.Query("MyTable").Where("p1", p1)).Returns(new SqlKata.Query());