diff --git a/Moq.Tests/CustomDefaultValueProviderFixture.cs b/Moq.Tests/CustomDefaultValueProviderFixture.cs index 5cd638a2e..d5ae24da2 100644 --- a/Moq.Tests/CustomDefaultValueProviderFixture.cs +++ b/Moq.Tests/CustomDefaultValueProviderFixture.cs @@ -89,8 +89,24 @@ public void Inner_mocks_inherit_custom_default_value_provider_from_outer_mock() Assert.Equal(expectedReturnValue, actualReturnValue); } + [Fact] + public void FluentMockContext_properly_restores_custom_default_value_provider() + { + var customDefaultValueProvider = new ConstantDefaultValueProvider(42); + var mock = new Mock() { DefaultValueProvider = customDefaultValueProvider }; + + mock.VerifySet(m => m.Inner.Value = 1, Times.Never); + // ^^^^^^^^^^^^^^^^^^^^^^ + // Moq has to execute this action to analyse what is being set. Because this is a multi-dot expression, + // it temporarily switches to DefaultValue.Mock. Once it's done, it should switch back to the one we + // set up above. + + Assert.Same(customDefaultValueProvider, mock.DefaultValueProvider); + } + public interface IFoo { + int Value { get; set; } int GetValue(); int[] GetValues(); IFoo Inner { get; } diff --git a/Source/FluentMockContext.cs b/Source/FluentMockContext.cs index 25fd3231d..654a86df2 100644 --- a/Source/FluentMockContext.cs +++ b/Source/FluentMockContext.cs @@ -100,14 +100,15 @@ public void Dispose() internal class MockInvocation : IDisposable { - private DefaultValue defaultValue; + private DefaultValueProvider defaultValueProvider; public MockInvocation(Mock mock, Invocation invocation, Match matcher) { this.Mock = mock; this.Invocation = invocation; this.Match = matcher; - defaultValue = mock.DefaultValue; + this.defaultValueProvider = mock.DefaultValueProvider; + // Temporarily set mock default value to Mock so that recursion works. mock.DefaultValue = DefaultValue.Mock; } @@ -120,7 +121,7 @@ public MockInvocation(Mock mock, Invocation invocation, Match matcher) public void Dispose() { - Mock.DefaultValue = defaultValue; + this.Mock.DefaultValueProvider = this.defaultValueProvider; } } }