0

I have following setup

A.CallTo(() => fakeChargeService
       .CreateAsync(A<ChargeCreateOptions>._, A<RequestOptions>._, A<CancellationToken>._))
 .Throws<StripeException>((se) => stripeException);

and then I assert

 var msg = await Assert.ThrowsAsync<StripeException>(async () => await mediator.Send(command, CancellationToken.None));

which eventually executes this piece of code

        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));

and here I get error

Assert.Throws() Failure Expected: typeof(Stripe.StripeException) Actual: typeof(FakeItEasy.Configuration.FakeConfigurationException): The faked method has the signature (Stripe.ChargeCreateOptions, Stripe.RequestOptions, System.Threading.CancellationToken), but throws was used with (Stripe.StripeException).

I am not sure what is it that I am doing wrong. Any help would be appreciated

epitka
  • 17,275
  • 20
  • 88
  • 141

1 Answers1

0

You seem to be specifying the wrong signature in your Throws. CreateAsync takes (Stripe.ChargeCreateOptions, Stripe.RequestOptions, System.Threading.CancellationToken), but Throws was used with (Stripe.StripeException).

See the second example in 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"));

Note that the signature of the lambda and the called method match.

You should update your lambda to match the proper signature. Or better yet, just replace with

.Throws<StripeException>(stripeException)

since there doesn't appear to be any reason to lazily throw, based on the snippet of code you provided.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111