Skip to content

Commit 575cf84

Browse files
dammejedstakx
authored andcommitted
Don't attempt to set CallBase on auto-generated mocks for delegates (#875)
1 parent 7564d0b commit 575cf84

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
1818
#### Fixed
1919

2020
* Regression: `SetupAllProperties` can no longer set up properties whose names start with `Item`. (@mattzink, #870; @kaan-kaya, #869)
21+
* Regression: `MockDefaultValueProvider` will no longer attempt to set `CallBase` to true for mocks generated for delegates. (@dammejed, #874)
2122

2223

2324
## 4.12.0 (2019-06-20)

src/Moq/MockDefaultValueProvider.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ protected override object GetFallbackDefaultValue(Type type, Mock mock)
3636
var mockType = typeof(Mock<>).MakeGenericType(type);
3737
Mock newMock = (Mock)Activator.CreateInstance(mockType, mock.Behavior);
3838
newMock.DefaultValueProvider = mock.DefaultValueProvider;
39-
newMock.CallBase = mock.CallBase;
39+
if(!type.IsDelegate())
40+
{
41+
newMock.CallBase = mock.CallBase;
42+
}
4043
newMock.Switches = mock.Switches;
4144
return newMock.Object;
4245
}

tests/Moq.Tests/Regressions/IssueReportsFixture.cs

+29
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,35 @@ public abstract partial class HttpContext
27132713

27142714
#endregion
27152715

2716+
#region 874
2717+
public class Issue874
2718+
{
2719+
[Fact]
2720+
public void MockDefaultValueProvider_will_Propagate_Callbase_to_nondelegates()
2721+
{
2722+
var mock = new Mock<IDictionary<string, IDictionary<string, string>>>()
2723+
{
2724+
CallBase = true,
2725+
DefaultValue = DefaultValue.Mock
2726+
};
2727+
var mockedIndexResult = mock.Object["foo"];
2728+
Assert.Null(mockedIndexResult["foo"]);
2729+
}
2730+
2731+
[Fact]
2732+
public void MockDefaultValueProvide_will_not_propagate_Callback_to_delegates()
2733+
{
2734+
var mock = new Mock<IDictionary<string, Func<string>>>()
2735+
{
2736+
CallBase = true,
2737+
DefaultValue = DefaultValue.Mock
2738+
};
2739+
var mockedIndexResult = mock.Object["foo"];
2740+
Assert.Null(mockedIndexResult());
2741+
}
2742+
}
2743+
#endregion
2744+
27162745
// Old @ Google Code
27172746

27182747
#region #47

0 commit comments

Comments
 (0)