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

Given mock.SetupGet(o => o.Property).CallBase(), strict mocks throw, demanding that the setup provide a return value #448

Closed
stakx opened this issue Sep 21, 2017 · 1 comment
Labels

Comments

@stakx
Copy link
Contributor

stakx commented Sep 21, 2017

Strict mocks verify that setups for property getters actually specify some return value. (For the same reason that C# enforces non-void methods to return a value if they don't throw an exception first, so that verification is perfectly reasonable.)

The following test fails:

public class Foo
{
    public virtual bool BooleanProperty => true;
}

[Fact]
var mock = new Mock<Foo>(MockBehavior.Strict);
mock.SetupGet(_ => _.BooleanProperty).CallBase();
Assert.True(mock.Object.BooleanProperty);

Moq.MockException: Foo.BooleanProperty invocation failed with mock behavior Strict.
Invocation needs to return a value and therefore must have a corresponding setup that provides it.
at Moq.ExecuteCall.ThrowIfReturnValueRequired(IProxyCall call, ICallContext invocation)
at Moq.ExecuteCall.HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
at Moq.Interceptor.Intercept(ICallContext invocation)
at Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.FooProxy.get_BooleanProperty()
at …

However, it should pass because BooleanProperty has been set up to return a value; not an explicitly specified value (.Returns(…)), but whatever the base class' (Foo's) BooleanProperty returns (.CallBase()).

@stakx
Copy link
Contributor Author

stakx commented Sep 21, 2017

This will be fixed in the next release of Moq (version > 4.7.99).

@stakx stakx closed this as completed Sep 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant