TypeError: Cannot read property 'apply' of undefined, using javascript apply in tests?
TypeError: Cannot read property 'apply' of undefined, using javascript apply in tests?
在 qunit 测试中使用 apply (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) 有限制吗?
import {
moduleForModel,
test
} from 'ember-qunit';
moduleForModel('enterprise', 'Enterprise Model', {});
test('salesTaxPercent gets converted from human readable decimal to float properly using convertPercentage helper method', function(assert) {
var store = this.store();
var model = this.subject({salesTaxPercent: 2.50});
assert.equal(model.get('salesTax'), 0.025, 'salesTax computed property is 0.025');
});
import DS from 'ember-data';
export default DS.Model.extend({
salesTax: DS.attr('number', {defaultValue: 0}),
salesTaxPercent: function(key, value, previousValue) {
return this.convertPercentage.apply(this, arguments);
}.property('salesTax')
});
DS.Model.reopen({
convertPercentage: function(key, value, previousValue) {
var percentKey = arguments[0].replace('Percent','');
if(arguments.length > 1) {
this.set(percentKey, accounting.unformat((arguments[1]/100)));
return Ember.isEmpty(this.get(percentKey)) ? null : accounting.unformat((this.get(percentKey) * 100).toFixed(2));
}
});
堆栈跟踪也如下所示:
TypeError: Cannot read property 'apply' of undefined
at null.<anonymous> (http://localhost:4200/provider/assets/inbox-dashboard.js:7099:42)
at Descriptor.computedPropertySet [as _set] (http://localhost:4200/provider/assets/vendor.js:27308:20)
at Descriptor.computedPropertySetWithSuspend [as set] (http://localhost:4200/provider/assets/vendor.js:27270:14)
at set (http://localhost:4200/provider/assets/vendor.js:32804:14)
at http://localhost:4200/provider/assets/vendor.js:33575:11
at tryFinally (http://localhost:4200/provider/assets/vendor.js:34504:28)
at changeProperties (http://localhost:4200/provider/assets/vendor.js:32529:7)
at setProperties (http://localhost:4200/provider/assets/vendor.js:33568:7)
at __exports__.default.Mixin.create.setProperties (http://localhost:4200/provider/assets/vendor.js:48068:16)
at Ember.Object.extend.createRecord (http://localhost:4200/provider/assets/vendor.js:119923:16)TypeError: Cannot read property 'apply' of undefined
at null.<anonymous> (http://localhost:4200/provider/assets/inbox-dashboard.js:7099:42)
at Descriptor.computedPropertySet [as _set] (http://localhost:4200/provider/assets/vendor.js:27308:20)
at Descriptor.computedPropertySetWithSuspend [as set] (http://localhost:4200/provider/assets/vendor.js:27270:14)
at set (http://localhost:4200/provider/assets/vendor.js:32804:14)
at http://localhost:4200/provider/assets/vendor.js:33575:11
at tryFinally (http://localhost:4200/provider/assets/vendor.js:34504:28)
at changeProperties (http://localhost:4200/provider/assets/vendor.js:32529:7)
at setProperties (http://localhost:4200/provider/assets/vendor.js:33568:7)
at __exports__.default.Mixin.create.setProperties (http://localhost:4200/provider/assets/vendor.js:48068:16)
at Ember.Object.extend.createRecord (http://localhost:4200/provider/assets/vendor.js:119923:16)
初始化程序不是 运行 在单元测试 componentForModel 中,因此 this.convertPercentage 未定义(如错误提示)。
所以我的建议是从您的模型中导入函数并附加它:
import convertPercentage from "../utils/convert-percentage";
export default DS.Model.extend({
salesTaxPercent: function(key, value, previousValue) {
return this.convertPercentage.apply(this, arguments);
}.property('salesTax')
});
https://github.com/rwjblue/ember-qunit/issues/149#event-261578363
在 qunit 测试中使用 apply (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) 有限制吗?
import {
moduleForModel,
test
} from 'ember-qunit';
moduleForModel('enterprise', 'Enterprise Model', {});
test('salesTaxPercent gets converted from human readable decimal to float properly using convertPercentage helper method', function(assert) {
var store = this.store();
var model = this.subject({salesTaxPercent: 2.50});
assert.equal(model.get('salesTax'), 0.025, 'salesTax computed property is 0.025');
});
import DS from 'ember-data';
export default DS.Model.extend({
salesTax: DS.attr('number', {defaultValue: 0}),
salesTaxPercent: function(key, value, previousValue) {
return this.convertPercentage.apply(this, arguments);
}.property('salesTax')
});
DS.Model.reopen({
convertPercentage: function(key, value, previousValue) {
var percentKey = arguments[0].replace('Percent','');
if(arguments.length > 1) {
this.set(percentKey, accounting.unformat((arguments[1]/100)));
return Ember.isEmpty(this.get(percentKey)) ? null : accounting.unformat((this.get(percentKey) * 100).toFixed(2));
}
});
堆栈跟踪也如下所示:
TypeError: Cannot read property 'apply' of undefined
at null.<anonymous> (http://localhost:4200/provider/assets/inbox-dashboard.js:7099:42)
at Descriptor.computedPropertySet [as _set] (http://localhost:4200/provider/assets/vendor.js:27308:20)
at Descriptor.computedPropertySetWithSuspend [as set] (http://localhost:4200/provider/assets/vendor.js:27270:14)
at set (http://localhost:4200/provider/assets/vendor.js:32804:14)
at http://localhost:4200/provider/assets/vendor.js:33575:11
at tryFinally (http://localhost:4200/provider/assets/vendor.js:34504:28)
at changeProperties (http://localhost:4200/provider/assets/vendor.js:32529:7)
at setProperties (http://localhost:4200/provider/assets/vendor.js:33568:7)
at __exports__.default.Mixin.create.setProperties (http://localhost:4200/provider/assets/vendor.js:48068:16)
at Ember.Object.extend.createRecord (http://localhost:4200/provider/assets/vendor.js:119923:16)TypeError: Cannot read property 'apply' of undefined
at null.<anonymous> (http://localhost:4200/provider/assets/inbox-dashboard.js:7099:42)
at Descriptor.computedPropertySet [as _set] (http://localhost:4200/provider/assets/vendor.js:27308:20)
at Descriptor.computedPropertySetWithSuspend [as set] (http://localhost:4200/provider/assets/vendor.js:27270:14)
at set (http://localhost:4200/provider/assets/vendor.js:32804:14)
at http://localhost:4200/provider/assets/vendor.js:33575:11
at tryFinally (http://localhost:4200/provider/assets/vendor.js:34504:28)
at changeProperties (http://localhost:4200/provider/assets/vendor.js:32529:7)
at setProperties (http://localhost:4200/provider/assets/vendor.js:33568:7)
at __exports__.default.Mixin.create.setProperties (http://localhost:4200/provider/assets/vendor.js:48068:16)
at Ember.Object.extend.createRecord (http://localhost:4200/provider/assets/vendor.js:119923:16)
初始化程序不是 运行 在单元测试 componentForModel 中,因此 this.convertPercentage 未定义(如错误提示)。
所以我的建议是从您的模型中导入函数并附加它:
import convertPercentage from "../utils/convert-percentage";
export default DS.Model.extend({
salesTaxPercent: function(key, value, previousValue) {
return this.convertPercentage.apply(this, arguments);
}.property('salesTax')
});
https://github.com/rwjblue/ember-qunit/issues/149#event-261578363