96

With Moq, is it valid to have more than one Matching Argument?

It.Is<string>() 

In this example I want the mockMembershipService to return a different ProviderUserKey depending on the User supplied.

mockMembershipService.Setup(
    x => x.GetUser(
      It.Is<string>(
        s => s.Contains("Joe")))
   .ProviderUserKey)
.Returns("1234abcd");


mockMembershipService.Setup(
  x => x.GetUser(
    It.Is<string>(
      s => s.Contains("Tracy")))
  .ProviderUserKey)
.Returns("5678efgh");

The SetUp defaults to the second statement rather than evaluating each on its own merits.

4 Answers 4

67

Isn't it confusing? You are trying to mock GetUser method but you set the Returns for that function's return value's property. You also want to state return type's property based on mocked method.

Here's a way a more clear way:

mockMembershipService.Setup(x => x.GetUser(It.IsAny<string>())
                     .Returns<string>(GetMembershipUser);

Here's a method to create the membership mock:

private MembershipUser GetMembershipUser(string s)
{
    Mock<MembershipUser> user =new Mock<MembershipUser>();
    user.Setup(item => item.ProviderUserKey).Returns(GetProperty(s));
    return user.Object;
}

Then you write a method for setting that property:

private string GetProperty(string s)
{
    if(s.Contains("Joe"))
        return "1234abcd";
    else if(s.Contains("Tracy"))
        return "5678efgh";
}
9
  • 1
    Will try this shortly, was looking at this video thethoughtfulcoder.com/blog/52/… while you added your answer which does something similar Feb 3, 2012 at 14:44
  • 1
    the code above will not compile it complains about Security.MembershipUser does not contain a reference for returns and also about User containing no definition for ProviderUserKey Feb 3, 2012 at 15:15
  • You should reference the assembly contaning Security.MembershipUser I guess. Or you can inject a dependency for creating users for you in your MembershipService Feb 3, 2012 at 15:19
  • 1
    I guess you can write another function to create a MembershipUser mock but it's seriously getting out of hand. I updated the code. Feb 3, 2012 at 15:37
  • 1
    Then you should consider changing your design. Depending on abstractions rather than concrete classes, refactoring your public interface, making your objects have a single responsibilty. Feb 3, 2012 at 15:44
58

If you want to restrict input to just "Joe" and "Tracy", you can specify multiple conditions in It.Is<T>(). Something like

mockMembershipService.Setup(x => x.GetUser(It.Is<String>(s => s.Contains("Joe") 
                                                         || s.Contains("Tracy")))
    .Returns<string>(/* Either Bartosz's or Ufuk's answer */);
2
  • its not that I want to restrict per se, I just want to evaluate what the input and return the desired output :-) Feb 3, 2012 at 15:54
  • Regardless of whether this was in the right spot, it helped me out, thanks @cadrell0 Jul 18, 2016 at 18:14
18

Succesive Setup calls nullify previous setups.

You could use your argument in your return callback:

mockMembershipService.Setup(x => x.GetUser(It.IsAny<string>()).ProviderUserKey).Returns<string>(s =>
{
    if(s.Contains("Joe"))
        return "1234abcd";
    else if(s.Contains("Tracy"))
        return "5678efgh";
});

If it's important to you to assert the argument passed, you also need It.Is<string>(...) instead of It.IsAny<string>(...).

4
  • I should get to try this out in about 2 hours. Feb 3, 2012 at 11:26
  • Ah well, I think it's because we're setting up a property here (ProviderUserKey), while the argument we're trying to act upon comes from GetUser(...). Can't check proper solution right now, but if you follow Ufuk advices, it should be ok...
    – Bartosz
    Feb 3, 2012 at 21:00
  • I'll give it a go over the weekend - thanks for your help! Not as straight forward as I first thought. Feb 3, 2012 at 21:16
  • @mattumotu: That's not what I experience, Moq just has a strange execution policy. Later added Setup calls are evaluated first (or always all are evaluated and last wins). It's just the opposite of what one would expect: Usually the first match is returned and the rest is ignored.
    – Christoph
    Nov 21, 2019 at 13:00
11

Please check Introduction to Moq > Matching Arguments documentation:

// matching Func<int>, lazy evaluated
mock.Setup(foo => foo.Add(It.Is<int>(i => i % 2 == 0))).Returns(true); 


// matching ranges
mock.Setup(foo => foo.Add(It.IsInRange<int>(0, 10, Range.Inclusive))).Returns(true); 


// matching regex
mock.Setup(x => x.DoSomething(It.IsRegex("[a-d]+", RegexOptions.IgnoreCase))).Returns("foo");
1
  • 3
    Now with more experience in Unit Tests, I'm not recommend this kind of approach. We should avoid putting logic within the Unit Tests. Another option is creating separated unit tests: one for Joe and other for Tracy
    – Jaider
    Nov 29, 2016 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.