Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird .net 4.0 exception when running unit tests

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??

like image 458
vdh_ant Avatar asked Apr 22 '10 22:04

vdh_ant


Video Answer


2 Answers

In the AssemblyInfo.cs of the referenced project add this following line

[assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]
like image 188
coderman Avatar answered Sep 17 '22 13:09

coderman


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

like image 26
UpTheCreek Avatar answered Sep 18 '22 13:09

UpTheCreek