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

AutoNSubstitute: Problem overriding Returns value #1160

Open
Ergamon opened this issue Jan 8, 2020 · 0 comments
Open

AutoNSubstitute: Problem overriding Returns value #1160

Ergamon opened this issue Jan 8, 2020 · 0 comments
Labels
bug NSubstitute An issue related to NSubstitute triage Something that's being investigated

Comments

@Ergamon
Copy link

Ergamon commented Jan 8, 2020

Today I stumbled about a bug and I am a little confused why it is happening.

The test code is around some bad legacy code which sadly cannot be changed:

 public interface IValue
  {
  }

  public interface IItem
  {
    IValue Value { get; }
  }

  public interface ISpecialItem : IItem
  {
    new IValue Value { get; set; }
  }

  public interface IFoo
  {
    IList<ISpecialItem> Items { get; }
  }

So basically there is an interface IItem having a readonly property and for some reason the writer of this legacy code wanted to have a setter for an inherited interface ISpecialItem. Therefore the new keyword.
In another interface IFoo is a property with a list of these special items as data type.

    [Fact]
    private void Test()
    {
      var customization = new AutoNSubstituteCustomization()
      {
        ConfigureMembers = true,
      };

      var fixture = new Fixture();
      fixture.Customize(customization);

      var foo = fixture.Create<IFoo>();

      // Reading the property once fixes it
      // var items = foo.Items; 

      foo.Items.ReturnsNull();
    }

In a test the interface IFoo is created with AutoFixture but someone wanted to test a special case where the property returns null.
For some strange reason an exception is thrown.

Can not return value of type IList`1 for ISpecialItem.set_Value (expected type Void).

Investigating the exception further I found that AutoFixture is trying to set the Value property of the special items to return null by calling NSubstitute. And NSubstitute throws an exception not allowing Returns statements on properties having a setter.

The fixture creates the IFoo proxy with an Items property returning 3 ISpecialItem proxies due to the ConfigureMembers being true in the customization.

By calling the ReturnsNull extension method I override the proxies return value of the Items property, but I dont understand why this results in a call to ISpecialItem.Value.ReturnsNull() ?

With my limited understanding of AutoNSubstitute it gets even stranger for me. If I read the property at some point before calling the Returns statement everything works fine.

If needed I can attach a sample solution.

Thx for any help
Gerald

@aivascu aivascu added bug triage Something that's being investigated NSubstitute An issue related to NSubstitute labels Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug NSubstitute An issue related to NSubstitute triage Something that's being investigated
Projects
None yet
Development

No branches or pull requests

2 participants