Skip to content

Exceptions by NSubstitute affect the next test #605

Closed
@tndata

Description

@tndata

Describe the bug
It is really hard to debug when errors/exceptions in one test affects the next test. Tests should be independent and not depend on previous tests. This really makes debugging impossible when a bug in one test makes the next test fail.

To Reproduce
The minimal code below reproduces the problem where the first test fails due to exception in the second test.

Yes, the code below is not really correct, but still the effect is unexpected. Yes, the analyzer picks it up, but I think the test should fail regardless:

public class UnitTest1
{
    public class User
    {
        public string UserName { get; set; }
    }

    public interface IDatabase
    {
        int CreateUser(string userName);
        User LoadUser(int userId);
    }

    [Fact]
    public void OKTestThatFail()
    {
        var stub = Substitute.For<IDatabase>();
    }

    [Fact]
    public void BadTestThatPass()
    {
        var stub = Substitute.For<IDatabase>();
        stub.Received(Arg.Any<int>());       
    }
}

Expected behaviour
I would expect that the second test in the code above fails due to bad usage of NSubstitute.

Environment:

  • NSubstitute version: [e.g. 4.2.1]

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions