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

GetMock<DbSet<MyClass>>() throws TargetInvocationException (EntityFrameworkCore) #104

Closed
demauk opened this issue Jan 14, 2021 · 4 comments · Fixed by #105
Closed

GetMock<DbSet<MyClass>>() throws TargetInvocationException (EntityFrameworkCore) #104

demauk opened this issue Jan 14, 2021 · 4 comments · Fixed by #105

Comments

@demauk
Copy link

demauk commented Jan 14, 2021

Hi,

This code was working on dotnet Core 3.1, AutoMocker 2.0.1:
var mockSet = mocker.GetMock<DbSet<MyClass>>();
DbSet<T> is declared in Microsoft.EntityFrameworkCore.

That line doesn't work in later versions of AutoMocker. They throw the following exception:

  Message: 
    System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
      ----> System.ArgumentNullException : Value cannot be null. (Parameter 'type')
  Stack Trace: 
    RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
    RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
    Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
    ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
    ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
    CastleProxyFactory.CreateProxy(Type mockType, IInterceptor interceptor, Type[] interfaces, Object[] arguments) line 56
    Mock`1.InitializeInstance() line 322
    Mock`1.OnGetObject() line 340
    Mock.get_Object() line 182
    Mock`1.get_Object() line 294
    GetMyFileProcessorTests.Setup() line 43
    --ArgumentNullException
    IntrospectionExtensions.GetTypeInfo(Type type)
    DbContext.ctor(DbContextOptions options)
    SurrogateContext.ctor(DbContextOptions`1 options) line 11
    SurrogateContextProxy.ctor(IInterceptor[] , DbContextOptions`1 options)

Checking out other issues, I saw the enablePrivate: true argument and gave that a try in v2.3, just in case. But it didn't help.

Is there a workaround?

Thanks!

@Keboo
Copy link
Collaborator

Keboo commented Jan 14, 2021

@demauk can you give this code a try:

var mock = new Mock<DbSet<MyClass>>();

var value = mock.Object;

From your stack trace it appears that the difference may be in the version of Moq, that is being used, not directly in this library.

@adamhewitt627
Copy link
Collaborator

I reproduced this in 2.3, but enablePrivate works. Can you check that again, and perhaps see if you can provide a reproduction where it still fails?

@demauk
Copy link
Author

demauk commented Jan 15, 2021

Apologies!

GetMock<DbSet<MyClass>>(true) does work. The problem was that enablePrivate: true exposed another error further down my Setup() and I assumed it was the same exception.

But this time, replacing x = mocker.GetMock with x = new Mock followed by mocker.Use(x) did the trick.

@adamhewitt627
Copy link
Collaborator

I'm hoping we have that fix in place soon so you don't have to specify the argument or call Use.

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.

3 participants