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

fix: Don't attempt to set CallBase on auto-generated mocks for delegates #875

Merged
merged 1 commit into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
#### Fixed

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


## 4.12.0 (2019-06-20)
Expand Down
5 changes: 4 additions & 1 deletion src/Moq/MockDefaultValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ protected override object GetFallbackDefaultValue(Type type, Mock mock)
var mockType = typeof(Mock<>).MakeGenericType(type);
Mock newMock = (Mock)Activator.CreateInstance(mockType, mock.Behavior);
newMock.DefaultValueProvider = mock.DefaultValueProvider;
newMock.CallBase = mock.CallBase;
if(!type.IsDelegate())
{
newMock.CallBase = mock.CallBase;
}
newMock.Switches = mock.Switches;
return newMock.Object;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,35 @@ public abstract partial class HttpContext

#endregion

#region 874
public class Issue874
{
[Fact]
public void MockDefaultValueProvider_will_Propagate_Callbase_to_nondelegates()
{
var mock = new Mock<IDictionary<string, IDictionary<string, string>>>()
{
CallBase = true,
DefaultValue = DefaultValue.Mock
};
var mockedIndexResult = mock.Object["foo"];
Assert.Null(mockedIndexResult["foo"]);
}

[Fact]
public void MockDefaultValueProvide_will_not_propagate_Callback_to_delegates()
{
var mock = new Mock<IDictionary<string, Func<string>>>()
{
CallBase = true,
DefaultValue = DefaultValue.Mock
};
var mockedIndexResult = mock.Object["foo"];
Assert.Null(mockedIndexResult());
}
}
#endregion

// Old @ Google Code

#region #47
Expand Down