I have generated an OData client from a Dynamics 365 metadata. I'm writing unit tests for the business logic and I want to mock the REST API with custom data.
Previously I asked how to set the fake return data for the ODataClient and I found an answer to it: How to set the return value of MS Fakes object?
Everything worked fine until I had to query nested data with the Expand keyword (using the Expand lambda from the OData Library).
For example, this query fails with NullReferenceException when used with the Faked ODataClient assembly:
IQueryable<Insurance> insurancesQuery = from i in _client.Insurances
.Expand(i => i.Account_id)
select i;
Without the Expand lambda, everything works as expected.
If I do this:
IQueryable<Insurance> insurancesQuery = from i in _client.Insurances
.Expand("account_id")
select i;
then I get a NullReferenceException
with a stack trace:
> at lambda_method(Closure , Ecr_insurance ) at
> System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at
> System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
> at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
I know the problem is that I haven't set up a Shim or Stub somewhere, but I just cannot find the correct method to replace. I've tried to read the OData source code but haven't found anything useful yet.
I've tried to set the Getter method of the Accounts (ShimSystem.AccountsGet
), but it doesn't do anything.
Any ideas how to approach this problem further?
It turns out that I have to also set the ExpandString
shim method on the OData Client's System
class to return the same data as the provider itself.