chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes

I have a test which asserts two conditions which are set on the same function using the AndArgumentsMatch predicate. The code is as follows:

    public interface IMyInterface
        int MyFunc(int intArg1);

    [TestMethod, Isolated]
    public void MatchArgumentsUsingPredicate
        var fake = Isolate.Fake.Instance<IMyInterface>();

        Isolate.WhenCalled((int arg1) => fake.MyFunc(arg1)).AndArgumentsMatch((arg1) => arg1 < 0 || arg1 >= 0).WillReturn(100);

        int result = fake.MyFunc(1);

        Assert.AreEqual(100, result);

        Isolate.WhenCalled((int arg1) => fake.MyFunc(arg1)).AndArgumentsMatch((arg1) => arg1 > 100).WillThrow(new ArgumentException());

        // Thow ArgumentException
At the moment, this test fails as the expected exception is never thrown. If I place each of the asserts in separate tests, then they both succeed. Am I trying to do something that shouldn't be done? 
In addition, is there an easier way to specify that an argument can match any integer?
asked by KBrown (1.7k points)

1 Answer

+1 vote
Best answer


The easier way to match any int is:

 Isolate.WhenCalled(() => fake.MyFunc(0)).WillReturn(100);

The value that you pass to MyFunc is ignored.

This solves your first issue as well.


You can also write cusom logic for MyFunc.

I prefer the way you initially did it, just thought you should know all possible implementations:


public void MatchArgumentsUsingPredicate2()
    var fake = Isolate.Fake.Instance<IMyInterface>();
    Isolate.WhenCalled(() => fake.MyFunc(0)).DoInstead(c =>
        if ((int)c.Parameters[0] > 100)
            throw new ArgumentException();
        return 100;
    int result = fake.MyFunc(1);
    Assert.AreEqual(100, result);
    // Thow ArgumentException


answered by alex (17k points)
selected by KBrown
Thanks Alex. That solves my issue.