如何假装抛出异常?
How to fake throwing an exception?
我有以下设置
A.CallTo(() => fakeChargeService
.CreateAsync(A<ChargeCreateOptions>._, A<RequestOptions>._, A<CancellationToken>._))
.Throws<StripeException>((se) => stripeException);
然后我断言
var msg = await Assert.ThrowsAsync<StripeException>(async () => await mediator.Send(command, CancellationToken.None));
最终执行这段代码
var policyResult = await Policy.Handle<StripeException>(x => x.ShouldRetry())
.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(0.5),
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
})
.ExecuteAndCaptureAsync(async () => await this.chargeService.CreateAsync(options, null, cancellationToken));
这里出现错误
Assert.Throws() 失败
预期:typeof(Stripe.StripeException)
实际:typeof(FakeItEasy.Configuration.FakeConfigurationException):伪造的方法具有签名 (Stripe.ChargeCreateOptions、Stripe.RequestOptions、System.Threading.CancellationToken),但 throws 与 (Stripe.StripeException) 一起使用。
我不确定我做错了什么。任何帮助将不胜感激
您似乎在 Throws
中指定了错误的签名。 CreateAsync
占用 (Stripe.ChargeCreateOptions, Stripe.RequestOptions, System.Threading.CancellationToken)
,但 Throws
与 (Stripe.StripeException)
一起使用。
参见Throwing Exceptions中的第二个例子:
// Pass up to 4 original call argument values into the method that creates the exception.
A.CallTo(() => fakeShop.NumberOfSweetsSoldOn(A<DateTime>._))
.Throws((DateTime when)=>new InvalidDateException(when + " is in the future"));
注意 lambda 的签名和调用的方法匹配。
您应该更新您的 lambda 以匹配正确的签名。或者更好的是,只需替换为
.Throws<StripeException>(stripeException)
因为根据您提供的代码片段,似乎没有任何理由懒惰地抛出。
我有以下设置
A.CallTo(() => fakeChargeService
.CreateAsync(A<ChargeCreateOptions>._, A<RequestOptions>._, A<CancellationToken>._))
.Throws<StripeException>((se) => stripeException);
然后我断言
var msg = await Assert.ThrowsAsync<StripeException>(async () => await mediator.Send(command, CancellationToken.None));
最终执行这段代码
var policyResult = await Policy.Handle<StripeException>(x => x.ShouldRetry())
.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(0.5),
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
})
.ExecuteAndCaptureAsync(async () => await this.chargeService.CreateAsync(options, null, cancellationToken));
这里出现错误
Assert.Throws() 失败 预期:typeof(Stripe.StripeException) 实际:typeof(FakeItEasy.Configuration.FakeConfigurationException):伪造的方法具有签名 (Stripe.ChargeCreateOptions、Stripe.RequestOptions、System.Threading.CancellationToken),但 throws 与 (Stripe.StripeException) 一起使用。
我不确定我做错了什么。任何帮助将不胜感激
您似乎在 Throws
中指定了错误的签名。 CreateAsync
占用 (Stripe.ChargeCreateOptions, Stripe.RequestOptions, System.Threading.CancellationToken)
,但 Throws
与 (Stripe.StripeException)
一起使用。
参见Throwing Exceptions中的第二个例子:
// Pass up to 4 original call argument values into the method that creates the exception.
A.CallTo(() => fakeShop.NumberOfSweetsSoldOn(A<DateTime>._))
.Throws((DateTime when)=>new InvalidDateException(when + " is in the future"));
注意 lambda 的签名和调用的方法匹配。
您应该更新您的 lambda 以匹配正确的签名。或者更好的是,只需替换为
.Throws<StripeException>(stripeException)
因为根据您提供的代码片段,似乎没有任何理由懒惰地抛出。