C# Mock - 条纹服务 - 返回 null

问题描述

我在对 Stripe 服务进行单元测试时遇到问题。

我有一个控制器,它调用我的服务,而这些又调用了条带 API。

我正在尝试模拟 PaymentIntentService 中的 CreateAsync 方法

我的服务:

   private readonly PaymentIntentService _paymentIntentService;

    #region Constructor


    public StripePaymentIntentService(Stripe.PaymentIntentService paymentIntentService)
    {
        _paymentIntentService = paymentIntentService;
    }

    public async Task<PaymentIntent> CreatePaymentIntentAsync(PaymentIntentCreateRequest request)
    {
        var paymentIntentCreateOptions = new PaymentIntentCreateOptions
        {
            Amount = request.Amount,Currency = request.Currency,Customer = request.StripeCustomerId,};

        if (request.IsHold)
            paymentIntentCreateOptions.CaptureMethod = "manual";

        if (request.IsSetupForFuturePayments)
            paymentIntentCreateOptions.SetupFutureUsage = "off_session";

        //I want to Mock the CreateAsync Method.
        var intent = await _paymentIntentService.CreateAsync(paymentIntentCreateOptions);

        return intent;
    }

我的测试:

public class StripePaymentIntentServiceShould
{

    #region members
    private Stripe.PaymentIntentCreateOptions request;
    private readonly StripePaymentIntentService _service;
    private readonly Mock<PaymentIntentService> _mockService;
    #endregion

    #region constructor
    public StripePaymentIntentServiceShould()        {

        _mockService  = new Mock<PaymentIntentService>();

        _mockService.Reset();

        request = new Stripe.PaymentIntentCreateOptions()
        {
            Amount = 123,Currency = ""
            CaptureMethod = "",SetupFutureUsage = "",Customer = "",};

        _mockService.Setup(service => service.CreateAsync(request,null,default)).Returns(Task.Fromresult(new PaymentIntent { Id = "dummy" }));
        _service = new StripePaymentIntentService(_mockService.Object);

  
    }
    #endregion

    #region methods

    [Fact]
    public async void CreatePaymentIntentAsync_Should()
    {
        //Arrange      
        PaymentIntentCreateRequest req = new PaymentIntentCreateRequest()
        {
            Amount = 123,Currency = "",IsHold = true,isSetupForFuturePayments = true,StripeCustomerId = "",};

        //Act
        var result = await _service.CreatePaymentIntentAsync(req);


        //Assert
        result.Id.Should().Be("dummy");
    }
    #endregion
}

但 CreateAsync 返回 null 而不是 PaymentIntent 对象。

你能帮我解决这个问题吗?

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)