开始使用 Moq 和 Nunit 时遇到困难

发布于 2024-09-14 15:01:21 字数 1384 浏览 4 评论 0原文

我把头撞在墙上,试图让一个非常简单的测试场景发挥作用。 我确信我错过了一些非常简单的东西!

无论我做什么,在针对我的 DLL 运行测试时,我似乎都会从 NUnit gui 中收到以下错误: System.TypeLoadException:程序集“DynamicProxyGenAssembly2,Version=0.0.0.0,Culture=neutral,PublicKeyToken=null”中的类型“Castle.Proxies.ITestProxy”正在尝试实现无法访问的接口。

现在我已经在 Stack Overflow 和其他地方查看时,我在很多地方看到了对这个错误的引用,但我不断找到的解决方案似乎没有帮助。在这个阶段我什至没有使用内部接口!我在该地方看到的解决方案是将以下行放入 AssemblyInfo.cs

[ assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

我正在使用:

  • Visual Studio 2010 Professional
  • c# 4.0
  • Moq 4.10810.8 Beta (bin 部署)
  • NUnit 2.5.5(安装在 GAC 中)

要重现此错误,我需要做的就是:

  1. 创建一个新的类库项目
  2. 引用 Moq 和 Unit(如上所述)
  3. 创建一个接口。我将我的接口称为 ITest,并将其公开,并且它有一个方法,即“string TestMethod();”。为了简单起见,我在本地项目中这样做。
  4. 创建一个名为“Testing”的类,用 [TextFixture] 装饰,并创建一个名为“TestMethod”的测试方法,用 [Test] 装饰。
  5. 构建项目,然后针对 Debug 文件夹中生成的 dll 运行 NUnit。

这是我的测试课程的内容

namespace MoqTest {
[TestFixture]
public class Testing {
    [Test]
    public void TestMethod() {

        var testMock = new Mock<ITest>();
        testMock.Setup(x => x.TestMethod()).Returns("String val");
        var xyz = testMock.Object;

        Assert.AreEqual(1, 1);

    }
}

}

---- 更新 --- 将起订量版本从 4.10810.8 更改为 4.0.10501.6 后,一切正常!

Banging my head against a wall trying to get a really simple testing scenario working.
I'm sure I'm missing something really simple!

Whatever I do, I seem to get the following error from the NUnit gui when running a test against my DLL:
System.TypeLoadException : Type 'Castle.Proxies.ITestProxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is attempting to implement an inaccessible interface.

Now I've seen reference to this error in heaps of places when looking in Stack Overflow and elsewhere, but the solution I keep finding doesn't seem to help. And I'm not even using an internal interface at this stage! The solution I see around the place is too put the following line in AssemblyInfo.cs

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

I'm using:

  • Visual Studio 2010 Professional
  • c# 4.0
  • Moq 4.10810.8 Beta (bin deployed)
  • NUnit 2.5.5 (Installed in GAC)

To recreate this error, all I need to do is:

  1. Create a new class library project
  2. Reference Moq and Unit (as above)
  3. Create an interface. I've called my interface ITest, made it public, and it has one method which is 'string TestMethod();'. Am doing this in the local project for simplicity.
  4. Create a class called 'Testing', decorated with [TextFixture] and a test method called 'TestMethod' decorated with [Test]
  5. Build the project, then run NUnit against the resulting dll in the Debug folder.

Here's the contents of my test class

namespace MoqTest {
[TestFixture]
public class Testing {
    [Test]
    public void TestMethod() {

        var testMock = new Mock<ITest>();
        testMock.Setup(x => x.TestMethod()).Returns("String val");
        var xyz = testMock.Object;

        Assert.AreEqual(1, 1);

    }
}

}

---- UPDATE ---
After changing Moq version from 4.10810.8 to 4.0.10501.6 everything works fine!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

絕版丫頭 2024-09-21 15:01:21

以下测试对我来说通过了:

public interface ITest { string TestMethod(); }

public class Testing
{
    [Test]
    public void TestMethod()
    {
        var testMock = new Mock<ITest>();
        testMock.Setup(x => x.TestMethod()).Returns("String val");
        var xyz = testMock.Object;

        Assert.AreEqual(1, 1);
    }
}

如果您的接口是公共的并且位于同一个程序集中,那么确实应该没有问题。我怀疑您只是在某处错过了可访问性关键字,因为非公共接口确实会引发运行时错误,因为代理程序集将无法实例化基于它的类型。

也许最好的办法是从我提供的代码开始,一次更改一件事,直到它与您失败的代码匹配。如果您在每次更改之间运行测试,我想您会发现缺少的内容。

如果您确实返回到内部接口,请注意您的 InternalsVisibleTo 语句必须位于与您的内部接口相同的程序集中,而不是您的测试程序集中。另请注意,如果您的程序集强命名,您可能需要添加InternalsVisibleTo 语句的公钥指纹为 MSDN 中描述

The following test passes for me:

public interface ITest { string TestMethod(); }

public class Testing
{
    [Test]
    public void TestMethod()
    {
        var testMock = new Mock<ITest>();
        testMock.Setup(x => x.TestMethod()).Returns("String val");
        var xyz = testMock.Object;

        Assert.AreEqual(1, 1);
    }
}

If your interface is public and in the same assembly, there really should be no problem. I suspect that you just missed an accessibility keyword somewhere, as a non-public interface does provoke a runtime error because the proxying assembly will be unable to instantiate a type based on it.

Probably the best thing to do is start with the code I've provided and change one thing at a time until it matches your failing code. If you run your test in between each change, I presume you'll find what was missing.

If you do go back to an internal interface, note that your InternalsVisibleTo statement must be in the same assembly as your internal interface, not your test assembly. Also note that if your assembly is strongly named you may need to add a public key fingerprint to your InternalsVisibleTo statement as described in MSDN.

黎夕旧梦 2024-09-21 15:01:21

是的,我对 NET40 的 Moq.4.0.10810.8 也有同样的问题...当我降级到版本 4.0.10531.7 时,一切又变绿了!

Yeah I had the same issue with Moq.4.0.10810.8 for NET40... When I downgraded to version 4.0.10531.7 everything went green again!

半世蒼涼 2024-09-21 15:01:21

我现在使用4.10.1,也遇到了同样的问题。我尝试降级到4.10.0,但没有成功。

我终于发现,虽然我使用的接口被标记为public,但它是在一个没有修饰符的类中。我发现有两件事使它起作用:

1)将接口拉到类之外。由于类不再将接口设为内部接口,因此程序集可以访问它。
2)将类标记为public。由于接口路径的所有部分都被标记为公共,因此程序集的访问没有问题。

这些策略在 4.10.0 和 4.10.1 中都有效。

I'm using 4.10.1 now, and I got this same issue. I tried downgrading to 4.10.0, but to no avail.

I finally found that, although the interface I was using was marked as public, it was in a class without a modifier. I found 2 things got it to work:

1) Pull the interface outside of the class. Because the class was no longer making the interface internal, it became accessible to the assembly.
2) Mark the class public. With all parts of the path to the interface being marked public, the assembly had no trouble accessing.

These strategies worked in both 4.10.0 and 4.10.1.

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