【发布时间】:2015-05-24 02:53:38
【问题描述】:
在使用 Moq 和 NUnit 为存储库编写单元测试时(遵循 this tutorial),我遇到了 TargetInvocationException,我不知道为什么会抛出它。
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);
TargetInvocationException 在最后一行被抛出。我在这里错过了什么明显的东西吗?我将如何解决这个问题?如果有人能解释一下我在这里做错了什么,我将不胜感激。
编辑:错误信息
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.
【问题讨论】:
标签: c# entity-framework moq