gpt4 book ai didi

nunit - 最小起订量 4.0 : The type initializer for 'Moq.Mock` 1' threw an exception

转载 作者:行者123 更新时间:2023-12-04 10:05:51 25 4
gpt4 key购买 nike

我得到了异常(exception)

The type initializer for 'Moq.Mock`1' threw an exception.



使用 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);
}
}
}

根据文档,一切都很好并且正确,并且编译得很好。当我尝试运行测试时,问题就来了。它到达上面标记的行并崩溃,但有以下异常:

The type initializer for 'Moq.Mock`1' threw an exception.



我看不出这里出了什么问题,有人可以解释一下吗?

最佳答案

当我将 CaSTLe.Core NuGet 包更新到版本 4.0.0 时,我发生了这种情况。目前最新的 Moq (v4.5.30) 无法正常工作的更改。

我通过回到 CaSTLe.Core 版本 3.3.3 解决了这个问题.

关于nunit - 最小起订量 4.0 : The type initializer for 'Moq.Mock` 1' threw an exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3843029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com