Im using Moq to create mocks of a data set.
I have created a little helper class that allows me to have an in memory storage instead of a database that makes unit testing a breeze. That way I can add and remove items from my mock data set, this allows me to test my insert and delete service calls.
During the setup of the mock I have a line that looks like the following
this.Setup(i => i.AcademicCycles).Returns(mockStore.GetList<AcademicCycle>());
My mock has a lot of properties so I would like to perform this setup step using reflection. I have managed to the Returns
part of the process working via reflection but I am stuck on the lambda method to Setup
.
Setup
takes an
Expression<Func<GoalsModelUnitOfWork, IQueryable<AcademicCycle>>>
that corresponds to the i => i.AcademicCycles
and I would like to create this dynamically. Using reflection I have the following:
The name of the property: "AcademicCycles"
The type IQueryable<AcademicCycle>
The type AcademicCycle
I also have the instance of the i
in the lambda statement which is a GoalsModelUnitOfWork
Expression<Func<...>>
if you statically know the parameter and return types. Internally Expression.Lambda does construct an instance of the appropriateExpression<Func<...>>
type, even though the return type of Expression.Lambda is weakly typed. – Chivy