enter code here
I have a method that logs errors when an exception is thrown within that method.
The method is non-static inside a singleton.
public object MethodA()
{
try
{
//code
SomeObj.Print(); //updated
}
catch (Exception ex)
{
log.Error(ex);
}
}
The unit test code below throws NullreferenceException:
var fakeLogger = A.Fake<ILog>();
MySingleton.Instance.Initialize(fakeLogger);
A.CallTo(() => MySingleton.Instance.MethodA()
.Invokes((x) => { throw new Exception(); });
//.Throws(new Exception()); --even this doesnt work
A.CallTo(() => fakeLogger.Error(A<object>.Ignored)).MustHaveHappened();
Stack trace:
at FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyInterceptionValidator.GetReasonForWhyMethodCanNotBeIntercepted(MethodInfo method)
at FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyInterceptionValidator.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, String& failReason)
at FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyGenerator.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, String& failReason)
at FakeItEasy.Creation.ProxyGeneratorSelector.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, String& failReason)
at FakeItEasy.Configuration.DefaultInterceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget)
at FakeItEasy.Configuration.FakeConfigurationManager.AssertThatMemberCanBeIntercepted(LambdaExpression callSpecification)
at FakeItEasy.Configuration.FakeConfigurationManager.CallTo(Expression`1 callSpecification)
at FakeItEasy.A.CallTo(Expression`1 callSpecification)
Solution: I had to make my non-fake method throw an exception and this is how I did.
var fakeLogger = A.Fake<ILog>();
var someObject = A.Fake<SomeObject>();
MySingleton.Instance.Initialize(fakeLogger);
A.CallTo(() => someObject.Print()).Throws(new Exception()); //key
MySingleton.Instance.MethodA();
A.CallTo(() => fakeLogger.Error(A<object>.Ignored)).MustHaveHappened();