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

CallBase should not be allowed for delegate mocks #708

Merged
merged 13 commits into from
Oct 15, 2018
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
#### Fixed

* `NullReferenceException` when using `SetupSet` on indexers with multiple parameters (@idigra, #694)
* `CallBase` should not be allowed for delegate mocks (@tehmantra, #706)


## 4.10.0 (2018-09-08)
Expand Down
5 changes: 5 additions & 0 deletions src/Moq/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public override void Execute(Invocation invocation)

public virtual void SetCallBaseResponse()
{
if (this.Mock.TargetType.IsDelegate())
{
throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resources.CallBaseCannotBeUsedWithDelegateMocks));
}

this.callBase = true;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Moq/MethodCallReturn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public override void SetCallbackResponse(Delegate callback)

public override void SetCallBaseResponse()
{
if (this.Mock.TargetType.IsDelegate())
{
throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resources.CallBaseCannotBeUsedWithDelegateMocks));
}

this.returnValueKind = ReturnValueKind.CallBase;
}

Expand Down
12 changes: 11 additions & 1 deletion src/Moq/Mock.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand All @@ -18,6 +19,7 @@

using Moq.Language;
using Moq.Language.Flow;
using Moq.Properties;

namespace Moq
{
Expand Down Expand Up @@ -164,7 +166,15 @@ private void CheckParameters()
public override bool CallBase
{
get => this.callBase;
set => this.callBase = value;
set
{
if (value && this.MockedType.IsDelegate())
{
throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resources.CallBaseCannotBeUsedWithDelegateMocks));
}

this.callBase = value;
}
}

internal override Dictionary<Type, object> ConfiguredDefaultValues => this.configuredDefaultValues;
Expand Down
9 changes: 9 additions & 0 deletions src/Moq/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Moq/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,7 @@ Expected invocation on the mock once, but was {4} times: {1}</value>
<value>The following invocations on mock '{0}' were not verified:
{1}</value>
</data>
</root>
<data name="CallBaseCannotBeUsedWithDelegateMocks" xml:space="preserve">
<value>CallBase cannot be used with Delegate mocks.</value>
</data>
</root>
46 changes: 46 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Data.Entity.Core.EntityClient;
#endif
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -2010,6 +2011,51 @@ public interface IMyInterface

#endregion

#region 706

public class Issue706
{
[Fact]
public void CallBase_should_not_be_allowed_for_void_delegate_mocks()
{
Mock<Action> mock = new Mock<Action>();
Language.Flow.ISetup<Action> setup = mock.Setup(m => m());

Exception ex = Assert.Throws<NotSupportedException>(() => setup.CallBase());
Assert.Equal(string.Format(CultureInfo.CurrentCulture, Resources.CallBaseCannotBeUsedWithDelegateMocks), ex.Message);
}

[Fact]
public void CallBase_should_not_be_allowed_for_non_void_delegate_mocks()
{
Mock<Func<bool>> mock = new Mock<Func<bool>>();
Language.Flow.ISetup<Func<bool>, bool> setup = mock.Setup(m => m());

Exception ex = Assert.Throws<NotSupportedException>(() => setup.CallBase());
Assert.Equal(string.Format(CultureInfo.CurrentCulture, Resources.CallBaseCannotBeUsedWithDelegateMocks), ex.Message);
}

[Fact]
public void CallBase_property_should_not_be_allowed_true_for_delegate_mocks()
{
Mock<Action> mock = new Mock<Action>();

Exception ex = Assert.Throws<NotSupportedException>(() => mock.CallBase = true);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, Resources.CallBaseCannotBeUsedWithDelegateMocks), ex.Message);
}

[Fact]
public void CallBase_property_should_be_allowed_false_for_delegate_mocks()
{
Mock<Action> mock = new Mock<Action>();
mock.CallBase = false;

Assert.Equal(false, mock.CallBase);
}
m-wild marked this conversation as resolved.
Show resolved Hide resolved
}

#endregion

// Old @ Google Code

#region #47
Expand Down