如何解决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 举报,一经查实,本站将立刻删除。