I'm trying to write unit tests but I'm having trouble figuring out how to delegate functions.
The application is an MVC application and the unit tests depend on moc data (they do not use the database).
We made a change to one of our services, and now the unit tests that test that service are failing. The adjustment I need to make to the unit tests don't seem to work, and this is what I need help with.
First of all, here's how it worked before the change:
The function in the service to be tested:
public Project GetProject(int projectId)
{
return _context.Projects.Find(projectId);
}
Substituting the delegate function in our unit tests:
protected override void Given()
{
GetMockFor<IRiskAliveContext>()
.Setup(ctx => ctx.Projects.Find(1))
.Returns(GetTestProject(1));
}
So essentially, we are say that whenever the service calls context.Projects.Find(1)
, return the mock project from GetTestProject(1)
.
This worked fine until we made our change:
public Project GetProject(int projectId)
{
return _context.Projects.Include("Report").FirstOrDefault(p => p.ProjectId == projectId);
}
It doesn't seem we can substitute a delegate function to calls to context.Projects.Include("report").FirstOrDefault(...)
, at least not in the same way as context.Project.Find(...)
. When I try to substitute the function as follows, I get a NotSupportedException
protected override void Given()
{
GetMockFor<IRiskAliveContext>()
.Setup(ctx => ctx.Projects.Include("Report").FirstOrDefault(p => p.ProjectId == 1))
.Returns(GetTestProject(1));
}
Is there a different way to substitute a delegate function when the call is to ...Include(...).FirstOrDefault(...)?