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

🐛 AutoFixture.AutoMoq appears to incorrectly handle object parameters marked as "in" #1336

Closed
3 of 6 tasks
IanKemp opened this issue Mar 23, 2022 · 1 comment
Closed
3 of 6 tasks
Labels

Comments

@IanKemp
Copy link

IanKemp commented Mar 23, 2022

Apologies in advance if this is unclear - the tests in the repro solution I've attached should be more eloquent than my writing.

I'm also not entirely sure if this is a problem with AutoMoq, or Moq itself - because I've had no issues with the described scenario with Moq when directly mocking the objects that seem to give AutoMoq issues, my guess is that it revolves around how AutoMoq uses Moq. If proven incorrect I'd be happy to re-file this against the Moq repo.

Describe the Bug

Given an object <OuterService>
That has a constructor dependency on an interface <InnerDependency>
And <InnerDependency> defines a method that accepts a parameter decorated with the in modifier
And <OuterService> defines a method that calls the previously-mentioned method on <InnerDependency>

When attempting to AutoMoq <OuterService> using the following:

var sut = new Fixture().Customize(new AutoMoqCustomization()).Create<<OuterService>>();

Then AutoMoq unexpectedly fails to generate the mocks in various ways, depending on the version of Moq involved.

  • Using latest AutoFixture.AutoMoq 4.17.0 with no explicit version of Moq installed, which implicitly pulls in Moq 4.7.0, the line of code above results in:
AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from <IInnerDependency> because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure.

Request path:
	<OuterService>
	  <IInnerDependency> service
	    <IInnerDependency>

Inner exception messages:
	System.ArgumentException: Type to mock must be an interface or an abstract or non-sealed class. 
	  System.TypeLoadException: Signature of the body and declaration in a method implementation do not match.  Type: 'Castle.Proxies.ObjectProxy'.  Assembly: 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.


---- System.ArgumentException : Type to mock must be an interface or an abstract or non-sealed class. 
-------- System.TypeLoadException : Signature of the body and declaration in a method implementation do not match.  Type: 'Castle.Proxies.ObjectProxy'.  Assembly: 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
  • Using latest AutoFixture.AutoMoq 4.17.0 with latest Moq 4.17.2, the given line yields:
  Message: 
System.ArgumentException : Type must not be ByRef (Parameter 'type')

  Stack Trace: 
TypeUtils.ValidateType(Type type, String paramName, Boolean allowByRef, Boolean allowPointer)
Expression.Constant(Object value, Type type)
MethodExpectation.CreateFrom(Invocation invocation) line 31
ReturnBaseOrDefaultValue.Execute(Invocation invocation) line 109
Return.Handle(Invocation invocation, Mock mock) line 172
IInterceptor.Intercept(Invocation invocation) line 29
Interceptor.Intercept(IInvocation underlying) line 107
AbstractInvocation.Proceed()
<IInnerDependency>Proxy.DoSomething(Poco& poco)
<OuterService>.DoSomething(String input) line 16
<TestMethod>() line 26
--- End of stack trace from previous location ---

but only if the in parameter on <IInnerDependency> is a non-primitive object type - if you use string, there is no exception (i.e. it works as expected)!

Scenario

Attached. There are 4 tests:

  • NoMoq_TestPoco: AutoFixture.AutoMoq 4.17.0, implicit Moq 4.7.0, POCO as the in parameter (fails)
  • NoMoq_TestString: AutoFixture.AutoMoq 4.17.0, implicit Moq 4.7.0, string as the in parameter (fails)
  • WithMoq_TestPoco: AutoFixture.AutoMoq 4.17.0, explicit Moq 4.17.2, POCO as the in parameter (fails)
  • WithMoq_TestString: AutoFixture.AutoMoq 4.17.0, explicit Moq 4.17.2, string as the in parameter (passes)

Expected Behavior

The scenario WithMoq_TestPoco succeeds, resulting in the generation of an appropriate mock as expected - it does not throw an exception.

Tasks

  • Updated all AutoFixture and related libraries
  • Confirmed in support forums if behavior is expected
@IanKemp IanKemp added the bug label Mar 23, 2022
@IanKemp
Copy link
Author

IanKemp commented Mar 23, 2022

Not a bug in AutoFixture.AutoMoq; is a bug in Moq. Closing.

@IanKemp IanKemp closed this as completed Mar 23, 2022
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