Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moq not verifying Expressions as expected #271

Closed
oisjack opened this issue Jun 27, 2016 · 1 comment
Closed

Moq not verifying Expressions as expected #271

oisjack opened this issue Jun 27, 2016 · 1 comment

Comments

@oisjack
Copy link

oisjack commented Jun 27, 2016

In reference to this question I asked on Stack Overflow, We're attempting to verify that a specific LINQ query was ran against a mock, but it fails no matter what we do. If I abridge the code being tested to the following:

public async Task<Activity> Get(long ID, Recruiter User, bool IsArchived = false)
{
    IAppQuery<Activity> result = collection.Where(x => x.ID == ID);

    return await result.FirstOrDefault();
}

And run the following test:

ActivitiesMock.Setup(x => x.Where(It.IsAny<Expression<Func<Activity, bool>>>())).Returns(queryMock.Object);
queryMock.Setup(x => x.FirstOrDefault()).ReturnsAsync(new Activity());

Activity result = await repo.Get(ID, User);

Assert.IsNotNull(result);

ActivitiesMock.Verify(x => x.Where(y => y.ID == ID));

The Assert.IsNotNull passes but the ActivitiesMock.Verify doesn't with the following error:

Test method Example.Tests.Backend.Repositories.ActivityRepositoryTests.GetDoesThings threw exception:
Moq.MockException:
Expected invocation on the mock at least once, but was never performed: x => x.Where(y => y.ID == .ID)

Configured setups:
x => x.Where(It.IsAny<Expression`1>()), Times.Once

Performed invocations:
IAppCollection`2.Where(x => (x.ID == value(Example.Backend.Repositories.ActivityRepository+<>c__DisplayClass2_0).ID))

Are we setting up a test incorrectly, are we expecting Moq to handle something it can't, or is this a bug in Moq?

@oisjack
Copy link
Author

oisjack commented Jun 27, 2016

Looks like it's because of the DisplayClass. Unless there's a recommendation on how to get them to compare, we're going with storing the Expression as part of the Returns statement then compiling and invoking the predicate against things that should and shouldn't match and asserting it the result is true or false.

@oisjack oisjack closed this as completed Jun 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant