How do I setup a mock that dynamically returns results based on a parameter's value?

Viewed 2647

I'm trying to test that an API controller method only returns users (plus some additional states which aren't relevant here) which match based on Name where Name = FirstName + ' ' + LastName.

I have a repository which exposes a GetUsersByName(name) method. I want to mock up a setup for that repository method which will return a List<UserModel> containing users which match the name criteria from some stubbed list of users (this.testUsers).

I've tried the following:

this.mockUserReposiotry.Setup(r => r.GetAllUsersByName(It.IsAny<string>()))
    .Returns(this.mockUtilities.MockUpUserModel(this.testUsers).Where(u => string.Concat(
        u.firstName, " ", u.lastName)
        .ToLower().Contains() );

but I don't know how to tie the Contains clause back to the IsAny<string> that I'm telling moq to match on.

Is this possible? I suspect that I need to provide IsAny with a parameter, but I can't find any similar examples.

1 Answers
Related