nsubstitute / NSubstitute

A friendly substitute for .NET mocking libraries.
https://nsubstitute.github.io
Other
2.65k stars 260 forks source link

Add info to AmbiguousArgumentsException #262

Closed dtchepak closed 6 years ago

dtchepak commented 7 years ago

From 260, would be useful to put info on what call is being run in AmbiguousArgumentsException, and also the arg matchers available vs. provided arguments (to narrow down what is ambiguous).

roryprimrose commented 7 years ago

I hit this problem because other tests in the class were substituting an extension method (part of a refactor). This error went away once I fixed up the substitutions.

Perhaps there needs to be some validation on the substitution request to validate that the member can be stubbed out. Should a separate issue be raised for this?

Another potential change (related to this issue) is to indicate in the exception message that other incorrect substitutions in the test run may be causing this issue.

dtchepak commented 7 years ago

Hi @roryprimrose, If I understand what you mean correctly, I don't think this is possible with NSub. The incorrect substitutions are only incorrect because NSub can't detect them (otherwise we would have the hooks to handle it properly).

If I've misunderstood or if you have some ideas on how we can detect these cases please let me know. :)

roryprimrose commented 7 years ago

Thanks @dtchepak

I tried to simulate this scenario but the error I got actually gave a good error message.

namespace ConsoleApp1
{
    using NSubstitute;

    class Program
    {
        static void Main(string[] args)
        {
            var target = Substitute.For<Stuff>();

            target.DoSomething().Returns(123);
        }
    }

    public static class Extensions
    {
        public static int DoSomething(this Stuff stuff)
        {
            return 1;
        }
    }

    public class Stuff
    {
        public string Value
        {
            get;
            set;
        }
    }
}

I don't understand why this code produces the nice failure from NSub but my more complex scenario didn't. Perhaps it was that there was a suite of tests being executed and that incorrect tests caused the others to fail. Either way, the linked issue helped me to figure out that I needed to fix the tests broken by my refactor to remove the problem.

At this stage I would say just ignore my previous comment :)

Thanks for the quick response.

dtchepak commented 6 years ago

Closing as part of a general cleanup of blocked issues. Please re-open if more information comes to light which will let us proceed with this.