views:

488

answers:

2

Hi guys I am receiving the following exception when trying to run my unit tests using .net 4.0 under VS2010 with moq 3.1.

Attempt by security transparent method 'SPPD.Backend.DataAccess.Test.Specs_for_Core.When_using_base.Can_create_mapper()' to access security critical method 'Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsNotNull(System.Object)' failed.

Assembly 'SPPD.Backend.DataAccess.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model. Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.

The test I am running is really straight forward and looks something like the following:

    [TestMethod]
    public void Can_create_mapper()
    {
        this.SetupTest();

        var mockMapper = new Moq.Mock<IMapper>().Object;
        this._Resolver.Setup(x => x.Resolve<IMapper>()).Returns(mockMapper).Verifiable();

        var testBaseDa = new TestBaseDa();
        var result = testBaseDa.TestCreateMapper<IMapper>();

        Assert.IsNotNull(result);  //<<< THROWS EXCEPTION HERE 
        Assert.AreSame(mockMapper, result);

        this._Resolver.Verify();
    }

I have no idea what this means and I have been looking around and have found very little on the topic. The closest reference I have found is this http://dotnetzip.codeplex.com/Thread/View.aspx?ThreadId=80274 but its not very clear on what they did to fix it...

Anyone got any ideas??

+2  A: 

MSDN: "The .NET Framework version 4 introduces new security rules that affect the behavior of the AllowPartiallyTrustedCallersAttribute attribute (see Security-Transparent Code, Level 2). In the .NET Framework 4, all code defaults to security-transparent, that is, partially trusted. However, you can annotate individual types and members to assign them other transparency attributes."

Haven't come across this myself, but perhaps you have imported somehing from a 3.5 project.

Check out these links:

Security Changes in the .NET Framework 4

Security-Transparent Code, Level 2

AllowPartiallyTrustedCallersAttribute Class

UpTheCreek
I don't think I imported anything from an existing project, as I created this solution/project from scratch in vs2010... The exception being thrown is on the result variable which is a dynamically created Moq object... I tried using the latest builds of Moq to see if it fixes the issues, but it didn't and as far as I know they are still building under 3.5...
vdh_ant
+1  A: 

This has been fixed in the latest version of Moq (it was a fix in DynamicProxy actually). Please give the latest v4 Beta a try.

http://moq.me

kzu