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

NS5000 when checking if an event subscription was received. #202

Closed
LiTo96 opened this issue Feb 13, 2023 · 4 comments · Fixed by #203
Closed

NS5000 when checking if an event subscription was received. #202

LiTo96 opened this issue Feb 13, 2023 · 4 comments · Fixed by #203
Labels
bug Something isn't working
Milestone

Comments

@LiTo96
Copy link

LiTo96 commented Feb 13, 2023

Since Version 1.0.16 the analyzer rule NS5000 alerts code like:
sub.Received(1).PropertyChanged += MyPropertyChangedHandler;

ErrorMessage:
NS5000: Unused received check. To fix, make sure there is a call after "Received". Correct: "sub.Received().SomeCall();". Incorrect: "sub.Received();"

@tpodolak tpodolak added the bug Something isn't working label Feb 14, 2023
@tpodolak
Copy link
Member

@dtchepak I am trying to add valid test case for this scenario and I came up with this

using NSubstitute;
using System;
using Xunit;

namespace MyNamespace
{
    public class Foo
    {
        public event Action SomeEvent;
    }

    public class FooTests
    {
        [Fact]
        public void Test()
        {
            var substitute = NSubstitute.Substitute.For<Foo>();
            substitute.Received().SomeEvent += Arg.Any<Action>();
        }
    }
}

However, this test passes, even though SomeEvent was not fired - according to docs
it is one of the way of asserting if event subscription was received. Is this valid test case in this case or there is regression in NSubstitute?

@dtchepak
Copy link
Member

This is because the member is not virtual. Running multiple tests including this one gives me:

  Error Message:
   NSubstitute.Exceptions.RedundantArgumentMatcherException : Some argument specifications (e.g. Arg.Is, Arg.Any) were left over after the last call.

If it is virtual it fails as expected.

  Error Message:
   NSubstitute.Exceptions.ReceivedCallsException : Expected to receive a call matching:
        SomeEvent += any Action
Actually received no matching calls.

@tpodolak
Copy link
Member

Ahh right. I dont know why I thought that events were handled differently. Will update my PR accordingly

@jm-atbas
Copy link

jm-atbas commented Feb 8, 2024

Can you release an update with this fix to NuGet, please?

@tpodolak tpodolak added this to the 1.0.17 milestone Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants