Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Moq It.Is<string>() Matching Arguments

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.

like image 248
Nicholas Murray Avatar asked Feb 03 '12 00:02

Nicholas Murray


3 Answers

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";
}
like image 115
Ufuk Hacıoğulları Avatar answered Oct 15 '22 17:10

Ufuk Hacıoğulları


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 */);
like image 51
cadrell0 Avatar answered Oct 15 '22 18:10

cadrell0


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>(...).

like image 18
Bartosz Avatar answered Oct 15 '22 18:10

Bartosz