enter code here
J'ai une méthode qui enregistre les erreurs lorsqu'une exception est levée dans cette méthode. La méthode est non statique dans un singleton.Test utilisant faux si une méthode enregistre une erreur lorsqu'une exception est levée dans cette méthode
public object MethodA()
{
try
{
//code
SomeObj.Print(); //updated
}
catch (Exception ex)
{
log.Error(ex);
}
}
Le code de test unitaire ci-dessous lancers francs 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: je devais faire ma méthode non-fake jeter une exception et voici comment je l'ai fait.
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();
Toujours me donne une exception de référence nulle sur la ligne "A.CallTo (() => MySingleton.Instance.MethodA()). Lance (nouvelle Exception());" –
Umm ... est MySingleton.Instance null? –
Merci! Mise à jour de ma solution –