In our test suite, in many places, we use a concrete object. In other places, we put a mock object.
Problem is once we register the Mock object we cant remove it.
There is NO call for
IOC.Dispose<>()
There is a call for
IOC.Dispose()
which we dont want to do.
So what we did was to register a null
IOC.Register((ITheObject>null);
When we do this, the next time we come around to
if(IOC.TryResolve<ITheObjectl>() == null)
{
IOC.Register<ITheObject>(new TheObject())
}
It throws an exception.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object parameters)
at NUnit.Framework.Internal.Reflect.Construct(Type type, Object arguments)
at NUnit.Framework.Internal.TypeWrapper.Construct(Object args)
at NUnit.Framework.Internal.Commands.ConstructFixtureCommand.<.ctor>b__0_0(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.BeforeTestCommand.Execute(TestExecutionContext context)
at NUnit.Framework.Internal.Execution.CompositeWorkItem.PerformOneTimeSetUp()
So now we are putting a try catch around the TryReslove, which works but is not optimal.
Is this the best way to do this. Is there a bug.