tags:

views:

139

answers:

2

Hi all,

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!

A: 

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.

ladenedge
Thanks for your effort, but even your example fails for me with the same error. (In a new project/solution with only this class in it!) So either I'm missing something else obvious or perhaps it's to do with the version of Moq/NUnit/.NET Framework.Can you confirm what versions you were using for your test?
Brendan
Further to this, the same code targeted at 3.5 with Moq 3.1.416 passes the test fine... So keen to know if your example works with .NET Framework 4.0
Brendan
Okay - so after more playing around, I've found that the version of Moq is making all the difference. Up till now, I was using 4.0.10810.8 and it was always failing with this error. As soon as I delete the reference to this version and then reference 4.0.10501.6 everything goes green! So thanks ladenedge for your response, I'll mark it as the answer because you are the only one who has tried to help me so far.
Brendan
Interesting. FWIW, I was using .NET 4, NUnit 2.5.3.9345 and Moq 4.0.812.4. Perhaps there is a bug with the 10810 Moq build? <shrug> Anyway, glad you found a workaround!
ladenedge
+2  A: 

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!

da2ce7
Thanks! I'm using this one too and had the same problem for the last two days. Downgrading solved it immediately.
reach4thelasers