Having trouble getting started with Moq and Nunit

Viewed 5090

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!

3 Answers

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.

Related