New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NSubstitute.ReceivedCalls() returning wrong value using AutoFixture.AutoNSubstitute #1007
Comments
Are you guys working on the same project? :) If so, could you clarify how you fixed the issue? The options suggested above should help with both the issues. If they don't - clarify the scenario. |
not actually the same project but at the same company. We already tried out some things and finally thought it must have something to do with autofixture - because of the frozen-attribute and a hard coded test execution count - or something like that. just as you described above I replaced the nuget 3.1 reference with the one from the fresh compiled nsubstitute project. after that I now have 118 instead of 178 failing tests because of Receiced(x) is still evaluating the wrong number of calls. my project is currently based on .net 4.5.2 with xunit2, current repo version of nsubstitute and autofixture 4.2 referenced. all of the tests are failing for the same two reasons - also there are still some receivedcalls failing in the unit tests. |
@dklinger @evilbaschdi Please follow up after you have a chance to check that - it's quite interesting why you sill see the issues, even after you applied the patch. Re-open the issue to indicate that we still have ongoing investigation. |
Yep, sure. Here it is: System under Test: public interface IRunSpeed
{
int Speed { get; }
void Dude();
int Dude2();
}
public class GetToDaChoppa
{
private readonly IRunSpeed _runSpeed;
public GetToDaChoppa(IRunSpeed runSpeed)
{
_runSpeed = runSpeed ?? throw new ArgumentNullException(nameof(runSpeed));
}
public int DoItNow
{
get
{
var runningSpeed = _runSpeed.Speed;
return 0;
}
}
} Test-Case: [Theory, AutoNSubstituteData]
public void DoItNowAsProperty_UsingAutoNSubstitute(
[Frozen] IRunSpeed runSpeed,
GetToDaChoppa sut)
{
// Arrange
runSpeed.Speed.Returns(49);
//Act
var x = sut.DoItNow;
//Assert
var tmp = runSpeed.Received(1).Speed;
Assert.Equal(1, runSpeed.ReceivedCalls().Count());
} Currently I'm trying to catch up on the discussions at xunit/xunit#1386 and #805. Maybe we're doing something wrong or there is a way to tell xUnit to not autogenerate the test name. public sealed class MyTheoryAttribute : TheoryAttribute
{
public MyTheoryAttribute([CallerMemberName] string memberName = null)
{
DisplayName = "MyTestCase";
}
} |
Hello, me again :) Thanks for your support. I've reopend an issue at the xUnit-Repo where the discussion should go on: xunit/xunit#1682 |
@dklinger Thanks for the detailed scenario. It's indeed quite tricky and it's hard to somehow work around it. From AutoFixture and NSubsitute perspective there no difference whether code is called somewhere deeply inside xUnit or in the test body. Usually, as a workaround you can use clear feature of NSubstitute:
This code should be run at the prologue of each test where you verify the exact number of calls. It works fine if you have a few tests, however obviously, if thousands of tests are affected, it will not help much 😅 It's a pity that NSubsitute + xUnit2 + AutoFixture integration doesn't work well and suffers from this kind of issues. AutoFixture product was designed to simplify the life, rather than to make it a nightmare 😕 Hope guys from xUnit will advice you a quick way to solve the issue for entire project. Let me know if you believe we can do something from our side to improve the situation. |
We have a lot of tests using 'Class.ReceivedCalls()' and 'var tmp = Class.Received(int).Property;' to check the call count. In v3 of AutoFixture it correctly reported the call count, especially of Properties, which it doesn't do anymore. The call count of methods seems to be still ok.
Given that we have the following code:
And given that we have the following tests:
My AutoNSubstituteDataAttribute looks like this:
The first test 'DoItNow_WithOutAutoNSubstitute' is working fine. But the second test 'DoItNow_UsingAutoNSubstitute' returns 2 for 'ios.Received(1).Speed;'.
It also returns 2 for 'runSpeed.ReceivedCalls();'.
Because of this we currently cannot upgrade our Solutions to v4 as we instantly have >1000 failing tests per solution. Any guidance on what the problem might be or where to look for the fix?
The text was updated successfully, but these errors were encountered: