【发布时间】:2020-09-19 00:07:21
【问题描述】:
我创建了一个允许我使用 Stripe.Net 类的服务(我称它为处理程序),但它并不是真正可测试的,因为在方法中我会实例化该类。例如:
public Customer CreateCustomer(string email)
{
var service = new CustomerService();
return service.Create(new CustomerCreateOptions
{
Email = email
});
}
这在尝试创建测试时很棒。所以我想我可以只使用 Stripe.net 中的类而不是创建处理程序。所以我尝试过这样做:
private static void AddTransients(IServiceCollection services)
{
services.AddTransient<Service<IStripeEntity>>();
services.AddTransient<ICreatable<IStripeEntity, BaseOptions>>();, BaseOptions));
}
我通过这将允许我像任何其他注入类一样在我的控制器周围传递类。但是当我启动我的应用程序时,我收到了这个错误:
无法为服务类型“Stripe.IStripeEntity”实例化实现类型“Stripe.IStripeEntity”。
所以我尝试像这样将类注册为通用类:
private static void AddTransients(IServiceCollection services)
{
services.AddTransient(typeof(Service<>));
services.AddTransient(typeof(ICreatable<,>));
}
但是当我运行它时,我得到了同样的错误。有谁知道我可以做些什么来让它工作?
【问题讨论】:
标签: c# .net-core stripe-payments