NSubstitute icon indicating copy to clipboard operation
NSubstitute copied to clipboard

Nested property calls across different mocks

Open dtchepak opened this issue 14 years ago • 1 comments


    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));
        }
    }

dtchepak avatar Sep 05 '11 07:09 dtchepak

I think that issue can be fixed by restoring previous values for last call router / last call at the end of ReturnConfiguredResultHandler.Handle execution

AlexanderSher avatar Sep 26 '14 22:09 AlexanderSher