MOQ 4.0:“Moq.Mock`1”的类型初始值设定项;抛出异常

发布于 2024-09-25 18:23:44 字数 1284 浏览 5 评论 0原文

我遇到了例外

“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自由如风 2024-10-02 18:23:44

当我将 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.

哭了丶谁疼 2024-10-02 18:23:44

进行以下更改后,我能够成功运行您的测试:

  1. TestSetup() 公开
  2. RunTest 中,更改 int val = obj.Valueint val = obj.GetValue() - 这只是为了让 Assert 通过。

我不熟悉 NUnit(我使用 xUnit),但我猜问题是 TestSetup() 是私有的。当该方法是私有的时,NUnit 会向我显示此异常:

Prototype.UnitTests.TestProgram.RunTest:
Invalid signature for SetUp or TearDown method: TestSetup

也许您使用的是旧版本的 NUnit,它以不同的方式处理这种情况(我刚刚下载了 2.5.7.10213)。

华泰

I was able to run your test successfully after making the following changes:

  1. Made TestSetup() public
  2. In RunTest, changed int val = obj.Value to int val = obj.GetValue() - this was just to get the Assert 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:

Prototype.UnitTests.TestProgram.RunTest:
Invalid signature for SetUp or TearDown method: TestSetup

Maybe you are using an older version of NUnit that handled this situation differently (I just downloaded 2.5.7.10213).

HTH

池木 2024-10-02 18:23:44

我在 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.

醉生梦死 2024-10-02 18:23:44

我在使用 Moq 时也遇到了这个问题,但与 Richard 略有不同。

我的错误如下。

消息:System.TypeInitializationException:“Moq.Mock`1”的类型初始值设定项引发异常。
---- System.IO.FileNotFoundException:无法加载文件或程序集“Microsoft.AspNetCore.Razor.Runtime,Version=2.1.1.0,Culture=neutral,PublicKeyToken=adb9793829ddae60”。

就我而言,我不必删除 Mock,而是以正确的版本安装缺少的程序集。我不知道为什么现在这只是一个问题。该问题发生在合并分支后,但两个分支都没有任何程序集,之前也没有显示此错误。不过,结局好一切都好。

I also had this issue using Moq, but slightly different than Richard.

My error was the following.

Message: System.TypeInitializationException : The type initializer for 'Moq.Mock`1' threw an exception.
---- System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文