Skip to content
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

DidNotReceive() and Received() results True if property is sring[] #618

Closed
KKrzaczek opened this issue Apr 7, 2020 · 5 comments
Closed

Comments

@KKrzaczek
Copy link

Describe the bug
Test should fail but its not.

To Reproduce
public class Test
{
public string[] Content { get; set; }
}

[Fact]
public void Should_What()
{
var obj = Substitute.For();
var test = obj.DidNotReceive().Content;
var test2 = obj.Received().Content;
}

Expected behaviour
I will be able to test property Content has been used

Environment:

  • NSubstitute version: 3.1.0
  • Platform: VS 2015, C#

Additional context
Add any other context about the problem here.

@dtchepak
Copy link
Member

dtchepak commented Apr 7, 2020

Hi @KKrzaczek,

If you are substituting for a class please make sure the members are virtual. See Substituting for classes and How NSubstitute works for more information.

Try it with:

public class Test
{
    public virtual string[] Content { get; set; }
}

Installing NSubstitute.Analyzers can help detect problems like this and suggest fixes.

@KKrzaczek
Copy link
Author

Thank you for the suggestion, it helps me.
If two opposing tests return the True at the same time, we have a bug.
You can explain why and indicate a solution, but this is a workaround.

In my opinion it should throw exception (ex. NotSupportedException, when a test is executed on non-virtual properties).
The testing environment is expected to detect problems. So he should be pessimistic.
In this case should be better return false than true. To sign the developer the problem (even if it is in the framework). This situation is in SQL when try to check null value, will always return false (null == true, null == 0 or even null == null).
But, it only me point of view.

@zvirja
Copy link
Contributor

zvirja commented Apr 7, 2020

@KKrzaczek Unfortunately, due to technical limitations NSubstitute simply cannot detect calls to non-virtual members. That's why we suggest installation of analyzers to cover our blind spots.

Closing the issue as there is nothing more we can do. Please let us know if you need further help.

@zvirja zvirja closed this as completed Apr 7, 2020
@dtchepak
Copy link
Member

dtchepak commented Apr 7, 2020

Thanks @zvirja! 😄I don't think I'm exaggerating to recommend that the C# or VB analyzers should be installed whenever someone installs the NSubstitute package.

@KKrzaczek, the analyzers will do one better than throwing a NotSupportedException; they will prevent the test with this bug from compiling (when project is using warnings as errors)! Highly recommended! 👍

@zvirja
Copy link
Contributor

zvirja commented Apr 8, 2020

@dtchepak Let's plan that for v5, as we discussed before. So that we have structure like xUnit - NSubstitute.Core and NSubstitute meta-package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants