在 angular 业力测试中模拟条纹
Mocking Stripe in angular karma tests
我在为 Karma 测试模拟 Stripe 对象时遇到问题。它必须是第一个加载的脚本(在 angular-stripe 之前)。
我正在我的业力配置中的脚本中加载它:
var stripe = new function() {
this.setPublishableKey = function(key) {}
}
Object.defineProperty(window, 'Stripe', { value: stripe, configurable: true, enumerable: true, writable: true });
这给出 Attempting to configurable attribute of unconfigurable property
我已经尝试过原型方法,但它无法识别我以这种方式添加的任何方法。
window.Stripe = function();
window.Stripe.prototype.setPublishableKey = function() {}
这给出:undefined is not a constructor (evaluating 'stripeProvider.setPublishableKey(config.stripeId)')
,我相信我已经追踪到不存在的方法(当我将 window.Stripe 转储到 angular-stripe 时,它没有显示该方法)
最后作为对象:
window.Stripe = {
...
产量:Stripe must be available as window.Stripe
。看起来 angular-stripe 特别想要一个功能。
如果我在本地复制 stripe 文件,无论 Stripe 做什么,我都会收到其他关于不在 stripe.com 上的错误,所以我想模拟它。
解决方案 感谢@estus,我能够以 angular 的方式解决这个问题:
angular.module('angular-stripe', []).provider('stripe', {
setPublishableKey: function() { },
$get: function() {}
})
beforeEach(module('app'));
以上足以覆盖实际加载的 angular-stripe
而不会抛出 Stripe missing 错误。
angular-条纹是 ultrathin wrapper around Stripe
global。 Angular DI 的主要优点之一是可测试性。
模拟 angular-stripe 单元而不是 Stripe 本身,它们正是为此而存在的。
module('app', ($provide) => {
$provide.provider('stripe', function () {
this.setPublishableKey = jasmine.createSpy();
this.$get = jasmine.createSpy();
});
});
我在为 Karma 测试模拟 Stripe 对象时遇到问题。它必须是第一个加载的脚本(在 angular-stripe 之前)。
我正在我的业力配置中的脚本中加载它:
var stripe = new function() {
this.setPublishableKey = function(key) {}
}
Object.defineProperty(window, 'Stripe', { value: stripe, configurable: true, enumerable: true, writable: true });
这给出 Attempting to configurable attribute of unconfigurable property
我已经尝试过原型方法,但它无法识别我以这种方式添加的任何方法。
window.Stripe = function();
window.Stripe.prototype.setPublishableKey = function() {}
这给出:undefined is not a constructor (evaluating 'stripeProvider.setPublishableKey(config.stripeId)')
,我相信我已经追踪到不存在的方法(当我将 window.Stripe 转储到 angular-stripe 时,它没有显示该方法)
最后作为对象:
window.Stripe = {
...
产量:Stripe must be available as window.Stripe
。看起来 angular-stripe 特别想要一个功能。
如果我在本地复制 stripe 文件,无论 Stripe 做什么,我都会收到其他关于不在 stripe.com 上的错误,所以我想模拟它。
解决方案 感谢@estus,我能够以 angular 的方式解决这个问题:
angular.module('angular-stripe', []).provider('stripe', {
setPublishableKey: function() { },
$get: function() {}
})
beforeEach(module('app'));
以上足以覆盖实际加载的 angular-stripe
而不会抛出 Stripe missing 错误。
angular-条纹是 ultrathin wrapper around Stripe
global。 Angular DI 的主要优点之一是可测试性。
模拟 angular-stripe 单元而不是 Stripe 本身,它们正是为此而存在的。
module('app', ($provide) => {
$provide.provider('stripe', function () {
this.setPublishableKey = jasmine.createSpy();
this.$get = jasmine.createSpy();
});
});