How to override the method Contains IQueryable during unit test?

So here's the thing: I have an application that I'm testing using LINQ to Entities (EF4 / .NET4). The application is associated with the implementation of the Contains method, which ignores NULL values ​​and, due to the database configuration, ignores case. This works great.

However, when I call the same methods from my unit tests, I go into a fake context that provides collections with IQueryable internal memory. In this case, this is the version of LINQ to Objects of Contains that is being introduced, and that it is of interest to zero and the case.

Now I can write my application code to check the zero and case, but I do not want to influence the SQL, which is generated in such a way that it will work when it is called from unit test, and SQL is not involved.

I really want to provide the correct IQueryable or whatever so that during the test I can change my own implementation of Contains, which ignores null and case. How should I do it? Thank!

+3
source share
2 answers

The compiler will be bound to the method derivation method with the highest derivative compiler.

Even if you pass the mock instance and have your own implementation of the “Contains” extension method in your type in your Unit Test context, you call some code that only knows that you have provided the IQueriable instance, so it communicates with the linq version for the objects.

, QueryProvider, , -, .

, , # 4.0.

+1

, mock IQueriable ?

0

Source: https://habr.com/ru/post/1744623/


All Articles