chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
hi

when i trying to apply TypeMock Isolator on code that uses PostSharp, it throws the following exception:
Test method PostSharpTypeMockTest.Test.ProgramTest.DoSomethingTest threw exception: System.TypeInitializationException: The type initializer for '<>AspectsImplementationDetails_1'þ threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object..

Stack Trace:
b2.a(Object A_0)
ax.a(Object A_0)
ax.a(String A_0, Object A_1)
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4)
TypeMock.InternalMockManager.isMocked(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
AspectsImplementationDetails_1..cctor()
PostSharpTypeMockTest.Program.DoSomething()
PostSharpTypeMockTest.Test.ProgramTest.DoSomethingTest()
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
PostSharpTypeMockTest.Test.ProgramTest.DoSomethingTest()

Used environment:
Windows 7 RC x64
Microsoft Visual Studio Team System 2008
PostSharp 1.5.6.629 x64
Typemock Isolator 5.43 x64

the same behavior also seen on the following environment:
Windows XP SP3 x86
Microsoft Visual Studio 2008 Developer Edition
PostSharp 1.5.6.629 x86
Typemock Isolator 5.43 x86

Sample C# Executable Code:
    [Serializable]
    public class TestAspect : OnMethodBoundaryAspect
    {

    }

    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main");
            DoSomething();
        }

        [TestAspect]
        public static void DoSomething()
        {
            Console.WriteLine("DoSomething");
            Do2();
        }

        public static void Do2()
        {
            Console.WriteLine("Do2");
        }
    }

Sample C# Test Code:
        [TestMethod()]
        [Isolated]
        public void DoSomethingTest()
        {
            Isolate.WhenCalled(() => Program.Do2())
                .DoInstead(
                    x => System.Console.WriteLine("Do2 Mocked")
                );
            Program.DoSomething();
        }


when i removing the [TestAspect] attribute, the test passing.
asked by mod (5.8k points)

7 Answers

0 votes
Hi,

Thanks for the detailed report, we'll try to reproduce it here and investigate it.

Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
I am having this same error so I will add few more details...

I am using...
Visual Studio 2008,
Windows XP,
Typemock 5.3.4,
Postsharp 1.5.6.629

This error occurs if you try to Isolate.Fake ANY object anywhere in the test class. Even if that object is not at all contained in the class that has postsharp aspects applied to it.

This is my postsharp aspect attribute
using System;
using PostSharp.Laos;
namespace Common.Aspect
{
    [Serializable]
    public class AtomicTransactionAttribute : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {

        }

        public override void OnExit(MethodExecutionEventArgs eventArgs)
        {
 
        }
    }
}


This is a class that is decorated with the attribute above
using Common.Aspect;
using System;

namespace Common.Test.Aspect
{
    public class NonIValidatorObject
    {
        public NonIValidatorObject()
        {
        }

        [AtomicTransaction]
        public void DoNothing()
        {
        }
    }
}


And here is the unit test code
using Common.Aspect;
using NUnit.Framework;
using TypeMock.ArrangeActAssert;
using System;

namespace Common.Test.Aspect
{
    [TestFixture]
    public class AtomicTransactionAttributeTests
    {
        [Test]
        public void SomeTest()
        {
            /*
             * Any attempt to fake any object will result in the same error
             * Notice that this object is NOT swapped and is not used anywhere 
             * Its just here to demostrate that trying to use typemock will cause an error
             */
            NonIValidatorObject obj = Isolate.Fake.Instance<NonIValidatorObject>();

            //DoNothing() is "intercepted" correctly with no error if the line of code above is deleted
            NonIValidatorObject target = new NonIValidatorObject();
            target.DoNothing();
        }
    }
}




This is the error that always happens
TestCase 'SomeTest'
failed: System.TypeInitializationException : The type initializer for '<>AspectsImplementationDetails_1' threw an exception.
----> System.NullReferenceException : Object reference not set to an instance of an object.
C:WorkCodesjdirect-common-libraryCommon.TestAspectNonIValidatorObject.cs(32,0): at Common.Test.Aspect.NonIValidatorObject.DoNothing()
C:WorkCodesjdirect-common-libraryCommon.TestAspectAtomicTransactionAttributeTests.cs(53,0): at Common.Test.Aspect.AtomicTransactionAttributeTests.SomeTest()
--NullReferenceException
at bh.a(Object A_0)
at ak.a(Object A_0)
at ak.a(String A_0, Object A_1)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4)
at TypeMock.InternalMockManager.isMocked(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at <>AspectsImplementationDetails_1..cctor()
answered by sjdirect (180 points)
0 votes
Thanks for the extra info - we are still investigating this issue, and this helps a lot.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
mod & sjdirect,

I believe I fixed the issue. I've sent you both a patched version which should work properly with PostSharp. Please post here if this works for you. Thanks for reporting this - it helped us fix a bug.

Thanks,
Doron
Typemock Support

PS this will be released in the next Isolator version, later this week.
answered by doron (17.2k points)
0 votes
Thanks Doron, this issue was resolved after installing the patch. Thanks for the quick response. Can you tell me what the error was (10,000 foot view)?
answered by sjdirect (180 points)
0 votes
Thanks, the patched version has solved the problem! :-)
answered by mod (5.8k points)
0 votes
Thanks, the patched version has solved the problem! :-)


mod & sjdirect,

I believe I fixed the issue. I've sent you both a patched version which should work properly with PostSharp. Please post here if this works for you. Thanks for reporting this - it helped us fix a bug.

Thanks,
Doron
Typemock Support

PS this will be released in the next Isolator version, later this week.


When will this new version be released? I have the same issue, but can see no new release of typemock from last week.
answered by filipham (180 points)
...