RedundantArgumentMatcherException using NSubstitute and NCrunch

1.9k Views Asked by At

I have a sample code (pasted below). I am using Xunit, NSubstitute and NCrunch. When I run the test in Visual Studio's Test Explorer, the test passes. When I debug, the test runs correctly. With NCrunch, I am facing a bizzare behavior, the test passes, and then fails, then it passes and then fails, and it continues like that.

[Fact]
public void Test()
{
    var drink = Substitute.For<IDrink>();
    var greetings = new Greetings();

    var derived = new Derived();
    derived.Test(drink, greetings);
}

public class Greetings
{
    public string Message { get; set; }
}

public interface IDrink
{
    void Prepare(Greetings greetings);
}

public abstract class Base
{
    public abstract void Test(IDrink drink, Greetings greetings);
}

public class Derived : Base
{
    public override void Test(IDrink drink, Greetings greetings)
    {
        drink.Prepare(greetings); /////////// The error is here
    }
}

public class NullDerived : Derived
{
    public override void Test(IDrink drink, Greetings greetings)
    {
        throw new Exception("No value found");
    }
}

The error NCrunch throwing is:

NSubstitute.Exceptions.RedundantArgumentMatcherException: Some argument specifications (e.g. Arg.Is, Arg.Any) were left over after the last call.

This is often caused by using an argument spec with a call to a member NSubstitute does not handle (such as a non-virtual member or a call to an instance which is not a substitute), or for a purpose other than specifying a call (such as using an arg spec as a return value). For example:....

I have tried to remove the parameter from Prepare(Greetings greetings) method, then NCrunch passes the test every single time.

As the error is suggesting, I am not passing the paramaters from the Test correctly.

My question is: What is the correct way of passing the greetings object to Prepare method. I have tried Arg.Any but it didn't work.

Any help is appreciated.

Edit 1: I am seeing the same behvarior using Reshaper's Unit Test Coverage.

0

There are 0 best solutions below