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

Somethings up with strict mock with recent changes #273

Closed
wyattwebb opened this issue Jul 20, 2016 · 10 comments · Fixed by #279
Closed

Somethings up with strict mock with recent changes #273

wyattwebb opened this issue Jul 20, 2016 · 10 comments · Fixed by #279

Comments

@wyattwebb
Copy link

When using strict moq I am getting inconsistent errors. I'm using MOQ and Autofixture in my main project where I am getting failed tests for Object.ToString() not being set up for a strict Mock. Using this in a vanilla project I get Object.GetHashCode not set up on a strict mock. I may be missing something easy but attached is a sample test project

Moq4516Tests.zip

Test Name: GetHashCodeTestFailure
Test FullName: Moq4516Tests.UnitTests.GetHashCodeTestFailure
Test Source: C:\Users\wwebb\Documents\Visual Studio 2015\Projects\Moq4516Tests\Moq4516Tests\UnitTest1.cs : line 54
Test Outcome: Failed
Test Duration: 0:00:00.3059009

Result StackTrace:
at Moq.ExtractProxyCall.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.ObjectProxy.GetHashCode()
at Moq.Interceptor.ExpressionKey.GetHashCode()
at System.Collections.Generic.ObjectEqualityComparer1.GetHashCode(T obj) at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary2.set_Item(TKey key, TValue value) at Moq.Interceptor.AddCall(IProxyCall call, SetupKind kind) at Moq.Mock.<>c__DisplayClass61_02.b__0()
at Moq.PexProtector.Invoke[T](Func1 function) at Moq.Mock.Setup[T,TResult](Mock1 mock, Expression1 expression, Condition condition) at Moq.Mock1.Setup[TResult](Expression`1 expression)
at Moq4516Tests.UnitTests.GetHashCodeTestFailure() in C:\Users\wwebb\Documents\Visual Studio 2015\Projects\Moq4516Tests\Moq4516Tests\UnitTest1.cs:line 60
Result Message:
Test method Moq4516Tests.UnitTests.GetHashCodeTestFailure threw exception:
Moq.MockException: Object.GetHashCode() invocation failed with mock behavior Strict.
All invocations on the mock must have a corresponding setup.

@syguts
Copy link

syguts commented Aug 8, 2016

I observe the same issue after upgrade from version 4.5.13 to 4.5.16. I get following exception:

Moq.MockException: Object.ToString() invocation failed with mock behavior Strict.
All invocations on the mock must have a corresponding setup.

Result stack trace is:
at Moq.ExtractProxyCall.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.ObjectProxy_2.ToString()
at Moq.ExpressionStringBuilder.ToStringConstant(ConstantExpression c)
at Moq.ExpressionStringBuilder.ToString(Expression exp)
at Moq.ExpressionStringBuilder.AsCommaSeparatedValues(IEnumerable1 source, Action1 toStringAction)
at Moq.ExpressionStringBuilder.ToStringMethodCall(MethodCallExpression node)
at Moq.ExpressionStringBuilder.ToString(Expression exp)
at Moq.ExpressionStringBuilder.ToStringLambda(LambdaExpression lambda)
at Moq.ExpressionStringBuilder.ToString(Expression exp)
at Moq.ExpressionStringBuilder.ToString()
at Moq.ExpressionStringBuilder.GetString(Expression expression, Func2 getTypeName) at Moq.ExpressionExtensions.ToStringFixed(Expression expression, Boolean useFullTypeName) at Moq.Interceptor.AddCall(IProxyCall call, SetupKind kind) at Moq.Mock.<>c__DisplayClass60_01.b__0()
at Moq.PexProtector.Invoke(Func1 function) at Moq.Mock.Setup(Mock1 mock, Expression1 expression, Condition condition) at Moq.Mock1.Setup(Expression`1 expression)

@kzu
Copy link
Contributor

kzu commented Aug 8, 2016

I just linked at the feature that caused this breakage. The ability to mock System.Object members was done like all other method setups, and it should be done always with a "loose mock" behavior instead, to avoid this very issue.

kolomanschaft pushed a commit to kolomanschaft/moq4 that referenced this issue Aug 10, 2016
@kzu kzu closed this as completed in #279 Aug 10, 2016
@BrianLunardini
Copy link

I still feel there is a potential regression (unless this functionality is by design).

The following code will execute without an exception on 4.5.13, but will throw on 4.5.19 ("Object.GetHashCode() invocation failed with mock behavior Strict"):

var mock = new Mock<IDisposable>(MockBehavior.Strict);
mock.Setup(x => x.Dispose());
mock.Object.GetHashCode();

Is that desired?

@kzu
Copy link
Contributor

kzu commented Aug 11, 2016

Really? @kolomanschaft?

@BrianLunardini
Copy link

Yeah @kzu, it appears that the same issue would occur with ToString() or Equals(). The LINQ queries in the InterceptObjectMethodsMixin are not properly checking for the presence of the designated object methods.

@kolomanschaft
Copy link

kolomanschaft commented Aug 11, 2016

Interesting @BrianLunardini. The test I wrote for those methods passed. It is past 8pm over here. I'll have a look first thing tomorrow morning.

@kzu
Copy link
Contributor

kzu commented Aug 11, 2016

Thanks @kolomanschaft !

kolomanschaft pushed a commit to kolomanschaft/moq4 that referenced this issue Aug 12, 2016
…ct methods in the interceptor context. Fixes devlooped#273 (again :-))
@kolomanschaft
Copy link

Accidentally used Select instead of Where. Funny thing is my test exploited a corner case where I didn't actually setup any calls to check in the if statements. Should be working now @BrianLunardini.

@wyattwebb
Copy link
Author

What nuget package will this be available in?

kzu pushed a commit that referenced this issue Aug 12, 2016
…ct methods in the interceptor context. Fixes #273 (again :-)) (#280)
@BrianLunardini
Copy link

Thanks @kolomanschaft!

@wyattwebb I just grabbed 4.5.21 from NuGet and verified it had the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants