locked
Unit test fails unless debugger attached

    Question

  • Hi Guys,

    I'm getting a peculiar result with some unit tests ...

    When running the unit test (in visual studio) it fails, so I re-run the unit test with the debugger attached to look deeper in to the issue.

    This time it passes.

    The error is:

    Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type SomeCustomType, key "" ---> System.TypeLoadException: Could not load type 'SomeOtherTypeThatSomeCustomTypeDependsOn'.

    I'm not sure if this is the right place to ask but I figured i'd ask in case any suggestions come up.

    Why would a type not be "loadable" when a debugger is not present as opposed to when it is?

    Seems odd to me.

    I am using the same build and config each time the only difference is i either push the "Run tests" button or the "Debug tests" button in the IDE.

    Small bit of additional info... i have managed to pull the unit test in question apart and find this ...

    given ...

    _kernel = new RhinoMocksMockingKernel();
    _locator = new NinjectAdapter.NinjectServiceLocator(_kernel);
    CommonService.Locator = _locator;

    When ...

    BounceService service = CommonService.Locator.GetInstance<BounceService>();

    then ...

    Assert.IsNotNull(service);

    The "when" (above) fails with an exception which reads ...

    Test method BounceServiceTest.BounceService_Object_Can_Be_Constructed threw exception: 
    Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type BounceService, key "" ---> System.TypeLoadException: Could not load type '...SubscriberContext' from assembly '...Business, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

    ... this only occurs when the debugger is NOT attached.

    Any ideas?

    • Edited by TheWardy Tuesday, April 10, 2012 3:31 PM
    Tuesday, April 10, 2012 3:12 PM

All replies

  • Hello TheWardy,

    Your issue seems strange to me as well. Could you please provide me one simple test project which can reproduce your issue? I would like to take a deeper look at it.

    Thanks.


    Vicky Song [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 12, 2012 2:42 AM
  • Are you running your tests with Code Coverage gathering enabled (or some of the other data collection tools)? If so, your codeunder test might be stuck in a proxy or might have been made public without you knowing it. When you run your test in the debugger, Code Coverage and some of the other data collectors are automatically disabled.

    This might explain the behavior you're seeing. We've had similar issues with Activator.CreateInstance and other Reflection based code that made assumptions on the fact whether things were public or private in the past with Code Coverage enabled.


    My blog: blog.jessehouwing.nl

    Friday, May 04, 2012 11:56 AM