TargetInvocationException thrown when trying to mock Entity Framework context

While writing a unit test using Moq & NUnit for a repository (following this tutorial), I encounter a TargetInvocationException and I have no clue why it's being thrown.

var fooList = new List<Foo>
{
    new Foo() { Id = 1, Name = "Something" },
    new Foo() { Id = 2, Name = "Some other thing" }
}.AsQueryable();

var mockedFooSet = new Mock<DbSet<Foo>>(fooList);
mockedFooSet.As<IQueryable<Foo>>().Setup(m => m.Provider).Returns(fooList.Provider);
mockedFooSet.As<IQueryable<Foo>>().Setup(m => m.Expression).Returns(fooList.Expression);
mockedFooSet.As<IQueryable<Foo>>().Setup(m => m.ElementType).Returns(fooList.ElementType);
mockedFooSet.As<IQueryable<Foo>>().Setup(m => m.GetEnumerator()).Returns(fooList.GetEnumerator()); 

var mockedContext = new Mock<FooContext>();
mockedContext.Setup(context => context.Foos).Returns(mockedFooSet.Object);

The TargetInvocationException is being thrown on that last line. Am I missing something ovbious here? How would I go about fixing this? I'd be thankful if anyone can give me some explanation of what I'm doing wrong here.

Edit: Error message

System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> System.TypeInitializationException : The type initializer for 'Castle.Proxies.DbSet`1Proxy' threw an exception.
----> System.ArgumentException : Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

Turned out there were some wrong DLL references. Deleting Moq completely and re-installing via Nuget fixed the problem.

'dbContextMock.Object' threw an exception of - MSDN, Reflection.TargetInvocationException' RRS feed I am using Entity Framework 7 in .Net core 1.0 rc2. So I have the class public class ApplicationDbContext : DbContext { public Have anybody tried moq.netcore Package? 4 TargetInvocationException thrown when trying to mock Entity Framework context Mar 23 '15 4 Angular Protractor end-2-end test fails after clicking a button Aug 11 '15 3 ASP.NET WebAPI 2 deserializing JSON sets some nested objects to null Jun 22 '15


Foos is a property; use mockedContext.SetupGet(c=>c.Foos).Return….

Using MockBehavior.Strict · Issue #20 · scott-xu/EntityFramework , I tried to make a mock for my simple DbContext and It works fine For TargetInvocationException : Exception has been thrown by the target of  How to Mock Entity Framework DbContext for Unit Testing. Posted by Anuraj on Tuesday, August 6, 2019 Reading time :2 minutes. EFCore UnitTesting. This post is about how to mock entity framework DbContext class for unit testing without any third party framework.


For me after updating Moq version 4.0.10827 to 4.10.1 the problem has disappeared.

zzzprojects/EntityFramework-Effort, I create a transient connection before each tests: It seems more an issue about how Unit Test are handled in VS2015, VS2017, and TargetInvocationException​: Exception has been thrown by the target of an invocation. We fixed case we found when testing effort with Entity Framework Extensions. AddRangeAsync is a method of Entity Framework Core and the purpose of this project is mocking an Entity Framework 6 context. As you can see in this post EF Core has an in-memory context that is useful for unit testing. Like Like


'dbContextMock.Object' threw an exception of type 'System , TargetInvocationException'. Category: visual I am using Entity Framework 7 in .​Net core 1.0 rc2. So I have the class public class ApplicationDbContext : DbContext { public Have anybody tried moq.netcore Package? The problem with unit testing code that uses Entity Framework classes, such as DbContext, is that the classes are difficult to mock. While the situation will improve with the introduction of Entity Framework 7 and its in-memory data store , for now we still have to find a way of mocking DbContext if we want to test code that uses it directly.


Entity Framework knowledge Base, After i have built my model i want to review it so i try to use EntityFramework TargetInvocationException: Exception has been thrown by the target of an invocati. show (apparently) a clear working example of mocking DbContext with EF 6,  Use a mocking framework to create test doubles – Using a mocking framework (such as Moq) you can have the in-memory implementations of your context and sets created dynamically at runtime for you. This article will deal with using a mocking framework. For creating your own test doubles see Testing with Your Own Test Doubles.


TargetInvocationException on mocking ChannelFactory with moq, Why TargetInvocationException is thrown when mocking You are receiving that error because ChannelFactory is trying to create actual proxies to non existing DbContext _db; private readonly DbSet<EmailQueue> _emailQueue; /* No  "exception has been thrown by the target of an invocation." I went on to debug and found that in the this exception comes when from c# code using reflection the code tries to invoke a function from an assembly which is written in c++.