nsubstitute / NSubstitute

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

UnexpectedArgumentMatcherException on Azure DevOps, nothing on local environment #682

Open dwekwejt opened 2 years ago

dwekwejt commented 2 years ago

Describe the bug I've got an UnexpectedArgumentMatcherException on my DevOps environment. I don't have it on my local computer.

[xUnit.net 00:00:01.60]     [anonymous content] [FAIL]
  Skipped [anonymous content] [1 ms]
  Failed [anonymous content] [17 ms]
  Error Message:
   NSubstitute.Exceptions.UnexpectedArgumentMatcherException : Argument matchers (Arg.Is, Arg.Any) should only be used in place of member arguments. Do not use in a Returns() statement or anywhere else outside of a member call.
Correct use:
  sub.MyMethod(Arg.Any<string>()).Returns("hi")
Incorrect use:
  sub.MyMethod("hi").Returns(Arg.Any<string>())
  Stack Trace:
     at NSubstitute.Core.ThreadLocalContext.LastCallShouldReturn(IReturn value, MatchArgs matchArgs)
   at NSubstitute.SubstituteExtensions.ConfigureReturn[T](MatchArgs matchArgs, T returnThis, T[] returnThese)
   at NSubstitute.SubstituteExtensions.Returns[T](T value, T returnThis, T[] returnThese)
   at [anonymous content] in [anonymous content]:line 57
--- End of stack trace from previous location ---

To Reproduce

[Fact]
public async Task Given_X_When_Start_Should_X()
{
    var model = new Model();

    var date = new DateTime(2022, 3, 14);

    var service = Substitute.For<IService>();
    service.GetDate()
        .Returns(date);

    var controller = new Controller(model, service);

    await controller.Start();
}

Please make sure you are using NSubstitute.Analyzers and that it does not pick up any problems with the reproduction code. The analyzers can help detect the cause of many issues.

Expected behaviour There shouldn't be any error on DevOps env.

Environment:

Additional context

dtchepak commented 2 years ago

Hi @dwekwejt ,

Could you please share Model, Controller and IService so I can try to investigate this? (Doesn't have to be the actual code, just something that reproduces the issue in the DevOps env.)

Also does this occur if the DevOps env just runs that single test? If there are other tests it could be a problem with an arg matcher in a previous test.

xtrem14 commented 9 months ago

Hi,

I'm having the same problem overriding an IOptions.Value.

The first initialization works correctly and when performing a second test I get the same error as you.

Sincerely,

cyrusdowney commented 5 months ago

So I'm also having this issue. It was in a larger project where I was converting from Moq to NSubstitute. First time using NSubstitute and I suspect that I did something wrong.

Though in playing around with it, I discovered that I could recreate the issue locally by changing the way XUnit handled threads.

I created and AssemblyInfo.cs file for the test project and added this.

using System.Runtime.InteropServices; using Xunit;

// In SDK-style projects such as this one, several assembly attributes that were historically // defined in this file are now automatically added during build and populated with // values defined in project properties. For details of which attributes are included // and how to customise this process see: https://aka.ms/assembly-info-properties

// Setting ComVisible to false makes the types in this assembly not visible to COM // components. If you need to access a type in this assembly from COM, set the ComVisible // attribute to true on that type.

[assembly: ComVisible(false)] [assembly: CollectionBehavior(DisableTestParallelization = true)]

In order to get ADO (azure dev ops) to build the test project, i had to modify the project and add the GenerateAssemblyInfo element and set it to false like below.

net8.0 Debug;Release;Release-Web;Release-Database;Release-ETL false false

I will be taking a more careful look at the documents and slowing converting this project, if i happens again I will update.

alexandrnikitin commented 5 months ago

@cyrusdowney Thank you for the info! Is there a chance you can share the test with us so that we can reproduce it locally?

cyrusdowney commented 5 months ago

No. There were a few hundred tests and it felt like my approach of converting everything at once was to much. So I took a step back and started over. I'm hoping that by slowly converting I can nail down the issue (create a reproduction for you etc.)

304NotModified commented 2 months ago

@cyrusdowney Is it OK to close this issue?

I will be taking a more careful look at the documents and slowing converting this project, if i happens again I will update.

Free free to re-open when you have more info on this one

cyrusdowney commented 2 months ago

Yep, I was not able to reproduce it and got though all the tests. Found multiple places where arg. Was used incorrectly, so who knows