UnexpectedArgumentMatcherException thrown when running all tests but no exception thrown when running class tests

12 views
Skip to first unread message

Dorra Bartaguiz

unread,
Dec 6, 2018, 4:27:28 PM12/6/18
to NSubstitute

Hello,


I’m using NSubstitute version 3.1.0

I don't understand the failure of my tests.

When I run all tests, I have two failing tests.

But if I run just some tests (red one included), they are all green.

There is no dependency between the tests

I pushed the code on my github https://github.com/iAmDorra/TestDouble


Do you have any idea please ?


Tks,

 

David Tchepak

unread,
Dec 6, 2018, 6:42:46 PM12/6/18
to nsubs...@googlegroups.com
Hello!

I think some arg matchers are used incorrectly in a few cases:


            Assert.ThrowsException<InvalidOperationException>(() =>
            {
                calculator.Divide(Arg.Any<int>(), denominator);
            });

I understand what is intended here: we don't really mind what argument is given to `Divide`, it should throw regardless. However `Arg.Any` and `Arg.Is` have a special meaning to NSubstitute. In this case it doesn't just mean "any arg for any call", it indicates that next time we are specifying a call NSubstitute will try to match any argument in that position. When a call gets stubbed in a later test, NSubstitute tries to match "any int" but can't find which argument to match (hence the "ambiguous arguments" exception). See http://nsubstitute.github.io/help/argument-matchers/#how_not_to_use_argument_matchers for more information.

This can be fixed by changing the above call and the one on line 47 to something like:

    var someInt = 1234;
    ...
    calculator.Divide(someInt, denominator);

I also recommend adding the C# NSubstitute Analyzer package to your test project: http://nsubstitute.github.io/help/nsubstitute-analysers/. I don't think this will catch this particular case, but it does pick up a few common gotchas with NSubstitute's API.

Regards,
David


--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at https://groups.google.com/group/nsubstitute.
For more options, visit https://groups.google.com/d/optout.

Dorra Bartaguiz

unread,
Dec 7, 2018, 5:13:43 AM12/7/18
to nsubs...@googlegroups.com
Tks David,

I always used Arg.Any() to ignore the value of the parameter :p
I understand now the use of it.

Tks,


You received this message because you are subscribed to a topic in the Google Groups "NSubstitute" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nsubstitute/FNm0LLnyhZE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nsubstitute...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages