NSubstitute
NSubstitute copied to clipboard
Nested property calls across different mocks
public class Issue56_NestedPropertyCalls
{
public interface IFoo
{
string StringProperty { get; set; }
}
[Test]
public void Nested_calls_to_properties()
{
const string expectedResult = "asldkfjdlkfj dslkfj";
var sub0 = Substitute.For<IFoo>();
var sub1 = Substitute.For<IFoo>();
sub1.StringProperty = expectedResult;
sub0.StringProperty.Returns(sub1.StringProperty);
//FAILS: Is equal to ""
//This is because Returns(..) can't tell it is meant to apply to sub0. Applies to last call (sub1) instead.
Assert.That(sub0.StringProperty, Is.EqualTo(expectedResult));
}
[Test]
public void Non_nested_calls()
{
const string expectedResult = "asldkfjdlkfj dslkfj";
var sub0 = Substitute.For<IFoo>();
var sub1 = Substitute.For<IFoo>();
sub1.StringProperty = expectedResult;
var prop = sub1.StringProperty;
sub0.StringProperty.Returns(prop);
//PASSES
Assert.That(sub0.StringProperty, Is.EqualTo(expectedResult));
}
}
I think that issue can be fixed by restoring previous values for last call router / last call at the end of ReturnConfiguredResultHandler.Handle execution