MOQ 4.0:“Moq.Mock`1”的类型初始值设定项;抛出异常
我遇到了例外
“Moq.Mock`1”的类型初始值设定项 抛出异常。
使用 Moq 4.0 我检查了几个论坛,他们提到使用 Moq-NoCastle 版本。我已经尝试过这个版本和 Moq 文件夹中的版本。两者的结果相同。
我有一个包含 2 个项目的解决方案,一个用于我的界面,一个用于我的测试。我的主项目有 2 个文件:
IMyInterface.cs:
using System;
namespace Prototype
{
public interface IMyInterface
{
int Value { get; set; }
void ProcessValue();
int GetValue();
}
}
我的 program.cs 文件仅包含随项目生成的默认代码。
我的测试项目有一个用于虚拟测试的文件 - TestProgram.cs
using System;
using NUnit.Framework;
using Moq;
namespace Prototype.UnitTests
{
[TestFixture]
public class TestProgram
{
Mock<IMyInterface> mock;
[TestFixtureSetUp]
void TestSetup()
{
mock = new Mock<IMyInterface>();
mock.Setup(x => x.GetValue()).Returns(2);
}
[Test]
public void RunTest()
{
IMyInterface obj = mock.Object; /* This line fails */
int val = obj.GetValue();
Assert.True(val == 2);
}
}
}
根据文档,一切都很好且正确,并且编译得很好。当我尝试运行测试时,问题就出现了。它到达上面标记的行并崩溃,但有例外:
“Moq.Mock`1”的类型初始值设定项 抛出异常。
我看不出这里出了什么问题,有人能解释一下吗?
I'm getting the exception
The type initializer for 'Moq.Mock`1'
threw an exception.
using Moq 4.0 I've checked around on a couple of forums and they allude to using the Moq-NoCastle version. I've tried both this and version in the Moq folder. Both with the same result.
I've got a solution with 2 projects, one for my interface, one for my tests. My main project has 2 files:
IMyInterface.cs:
using System;
namespace Prototype
{
public interface IMyInterface
{
int Value { get; set; }
void ProcessValue();
int GetValue();
}
}
My program.cs file has just the default code that's generated with the project.
My test project has a single file for my dummy test - TestProgram.cs
using System;
using NUnit.Framework;
using Moq;
namespace Prototype.UnitTests
{
[TestFixture]
public class TestProgram
{
Mock<IMyInterface> mock;
[TestFixtureSetUp]
void TestSetup()
{
mock = new Mock<IMyInterface>();
mock.Setup(x => x.GetValue()).Returns(2);
}
[Test]
public void RunTest()
{
IMyInterface obj = mock.Object; /* This line fails */
int val = obj.GetValue();
Assert.True(val == 2);
}
}
}
According to the documentation all is good and proper, and it compiles nicely. The problem comes when I try to run the test. It gets to the line marked above and crashes with the exception:
The type initializer for 'Moq.Mock`1'
threw an exception.
I can't see what's going wrong here, can anyone shed some light on it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当我将 Castle.Core NuGet 包更新到版本 4.0.0 时,我遇到了这种情况。目前,某些变化无法与最新的 Moq (v4.5.30) 正常工作。
我通过返回 Castle.Core 版本 3.3.3 解决了这个问题。
This occurred to me when I updated the Castle.Core NuGet Package to the version 4.0.0. Something changed that is does not work properly with latest Moq (v4.5.30) at this moment.
I solved this by going back to the Castle.Core version 3.3.3.
进行以下更改后,我能够成功运行您的测试:
TestSetup()
公开RunTest
中,更改int val = obj.Value
到int val = obj.GetValue()
- 这只是为了让Assert
通过。我不熟悉 NUnit(我使用 xUnit),但我猜问题是 TestSetup() 是私有的。当该方法是私有的时,NUnit 会向我显示此异常:
也许您使用的是旧版本的 NUnit,它以不同的方式处理这种情况(我刚刚下载了 2.5.7.10213)。
华泰
I was able to run your test successfully after making the following changes:
TestSetup()
publicRunTest
, changedint val = obj.Value
toint val = obj.GetValue()
- this was just to get theAssert
to pass.I'm not familiar with NUnit (I use xUnit), but my guess is TestSetup() being private was the problem. When that method is private, NUnit shows this exception for me:
Maybe you are using an older version of NUnit that handled this situation differently (I just downloaded 2.5.7.10213).
HTH
我在 Moq 方面也遇到了类似的例外(以前工作得很好)。
对我来说,解决方案是使用 NuGet 卸载 Moq 和异常提到的程序集。然后使用 NuGet 重新安装 Moq 并应用随后出现的任何 NuGet 更新。
I had a similar exception with Moq (it had previously worked fine).
For me the solution was to use NuGet to uninstall Moq and the assembly that the exception mentioned. And then re-install Moq using NuGet and apply any NuGet updates that subsequently appeared.
我在使用 Moq 时也遇到了这个问题,但与 Richard 略有不同。
我的错误如下。
就我而言,我不必删除 Mock,而是以正确的版本安装缺少的程序集。我不知道为什么现在这只是一个问题。该问题发生在合并分支后,但两个分支都没有任何程序集,之前也没有显示此错误。不过,结局好一切都好。
I also had this issue using Moq, but slightly different than Richard.
My error was the following.
In my case I didn't have to remove Mock, install the missing Assembly in the correct version. I don't know why this was only an issue now. The issue happend after merging branches, but both branches didn't have either assemblies nor showed this error before. However, end good all good.