我正在尝试对使用StripeJS
的付款组件进行单元测试。
我将其导入文件'ng-app.js':
stripe: /*@ngInject*/ function ($ocLazyLoad) {
return $ocLazyLoad.load({
type: 'js',
path: 'https://js.stripe.com/v3/'
});
}
在我使用的付款组件(payment.component.ts
)内部:
import {StripeJS} from 'stripejs';
import {ElementFactory, OnChange, StripeElement} from 'stripejs/element';
还有:
constructor(private fb: FormBuilder, private store: Store<AppState>) {
this.stripe = window.Stripe(window.properties.stripePKs[this.carrier]);
this.createStripeElements(this.stripe.elements());
}
但是我不知道如何在StripeJS
内插入payment.component.spec.ts
。
当我执行undefined
时,组件为component = fixture.componentInstance;
我的要求是知道如何在测试中将StripeJS
组合为模拟或不模拟,以便我为其余测试成功创建组件。
我从未使用stripeJS,所以我将为您提供一个一般性的答案,如果您需要一个具体的示例,请在此处或github上发布代码,我们可以为您提供更多详细信息。在进行单元测试之前,您需要了解Jasmine和Karma的工作方式。在单元测试的导入部分中,确保已导入stripeJS。在你的UT构造函数有类似mockstripejs:Mock<StripeJS>
在要进行测试的测试类中,您需要设置要测试的条带操作,如果它类似于stripe.getcardprovider
,则需要诸如mockstripejs.setup(x=>x.getcardprovider).is(cardProvider.Object)
这样的设置其中cardProvider是要返回的对象希望对您有所帮助。