Qunit _initProperties 不是函数
Qunit _initProperties is not a function
我 运行 遇到了一些奇怪的行为,想看看是否有人知道为什么会这样。我编写了一个简单的 Ember QUnit 测试,并希望在每个测试之间共享一些数据,以减少混乱。
测试
import Ember from 'ember'
import { moduleFor, test } from 'ember-qunit';
let create = Ember.Object.create;
let shared = create({});
shared.stardardData1 = create({ id: 1 });
shared.stardardData2 = create({ id: 2 });
moduleFor('controller:foo', 'description', {
beforeEach() { ... }
afterEach() { ... }
}
test('should do things', function () {
let myGroup = [shared.standardData1, shared.standardData2];
}
这里有几件事:
- 我收到一个错误
this._initProperties is not a function
- 删除共享内容后错误消失
- 如果我在
moduleFor
和 test
之间添加共享内容,错误仍然存在
- 我希望能够共享在 beforeEach 或全局中定义的变量
- 我试过在
beforeEach
中做类似 let a = 1
的事情,但似乎无法在测试本身中引用它们
我注意到 QUnit 文档说他们已经消除了全局变量。这可能在其中发挥作用吗? https://qunitjs.com/upgrade-guide-2.x/
PS: 如果能有一些只设置一次模块而不是每次都设置模块的东西就好了
编辑
这是堆栈跟踪:
Promise rejected before should do things: this._initProperties is not a function
Source:
TypeError: this._initProperties is not a function
at create (http://localhost:7357/assets/vendor.js:46461:14)
at Object.beforeEach (http://localhost:7357/assets/tests.js:185:20)
at http://localhost:7357/assets/test-support.js:6586:31
at tryCatch (http://localhost:7357/assets/vendor.js:61631:14)
at invokeCallback (http://localhost:7357/assets/vendor.js:61646:15)
at publish (http://localhost:7357/assets/vendor.js:61614:9)
at http://localhost:7357/assets/vendor.js:41408:7
at invoke (http://localhost:7357/assets/vendor.js:11120:16)
at Object.flush (http://localhost:7357/assets/vendor.js:11184:11)
at Object.flush (http://localhost:7357/assets/vendor.js:10992:17)
实际上我认为这可能与全局变量无关。我还添加了(之前不包括)let create = Ember.Object.create
只是为了节省一些输入(并用调用 create(object)
包装上面的对象。摆脱它并使用长格式似乎摆脱了虽然这个错误...
你问题的最后一部分是实际问题。
当你
let create = Ember.Object.create
您从 Ember.Object
中提取函数 create
并创建一个新的引用,作为一个普通函数。
根据 JS 绑定规则,当您将函数作为普通函数调用时,它内部的 this
绑定到全局对象(或 undefined
在严格模式下)。另一方面,当您将函数作为对象上的方法调用时(即直接在 Ember.Object
上调用它)会将 this
绑定到该对象。
这就是 this
在 create
中未定义的原因,因此 this._initProperties
不是一个函数。
这是一个正在发生的事情的演示:
// an object with a method
var obj = {
getThis: function() {
return this;
}
};
// an extracted reference to the method
var extractedGetThis = obj.getThis;
// this binds to the object
console.log(
obj.getThis() === obj // true
);
// this binds globally
console.log(
extractedGetThis() === window // true
);
我 运行 遇到了一些奇怪的行为,想看看是否有人知道为什么会这样。我编写了一个简单的 Ember QUnit 测试,并希望在每个测试之间共享一些数据,以减少混乱。
测试
import Ember from 'ember'
import { moduleFor, test } from 'ember-qunit';
let create = Ember.Object.create;
let shared = create({});
shared.stardardData1 = create({ id: 1 });
shared.stardardData2 = create({ id: 2 });
moduleFor('controller:foo', 'description', {
beforeEach() { ... }
afterEach() { ... }
}
test('should do things', function () {
let myGroup = [shared.standardData1, shared.standardData2];
}
这里有几件事:
- 我收到一个错误
this._initProperties is not a function
- 删除共享内容后错误消失
- 如果我在
moduleFor
和test
之间添加共享内容,错误仍然存在
- 我希望能够共享在 beforeEach 或全局中定义的变量
- 我试过在
beforeEach
中做类似let a = 1
的事情,但似乎无法在测试本身中引用它们
我注意到 QUnit 文档说他们已经消除了全局变量。这可能在其中发挥作用吗? https://qunitjs.com/upgrade-guide-2.x/
PS: 如果能有一些只设置一次模块而不是每次都设置模块的东西就好了
编辑
这是堆栈跟踪:
Promise rejected before should do things: this._initProperties is not a function Source: TypeError: this._initProperties is not a function at create (http://localhost:7357/assets/vendor.js:46461:14) at Object.beforeEach (http://localhost:7357/assets/tests.js:185:20) at http://localhost:7357/assets/test-support.js:6586:31 at tryCatch (http://localhost:7357/assets/vendor.js:61631:14) at invokeCallback (http://localhost:7357/assets/vendor.js:61646:15) at publish (http://localhost:7357/assets/vendor.js:61614:9) at http://localhost:7357/assets/vendor.js:41408:7 at invoke (http://localhost:7357/assets/vendor.js:11120:16) at Object.flush (http://localhost:7357/assets/vendor.js:11184:11) at Object.flush (http://localhost:7357/assets/vendor.js:10992:17)
实际上我认为这可能与全局变量无关。我还添加了(之前不包括)let create = Ember.Object.create
只是为了节省一些输入(并用调用 create(object)
包装上面的对象。摆脱它并使用长格式似乎摆脱了虽然这个错误...
你问题的最后一部分是实际问题。
当你
let create = Ember.Object.create
您从 Ember.Object
中提取函数 create
并创建一个新的引用,作为一个普通函数。
根据 JS 绑定规则,当您将函数作为普通函数调用时,它内部的 this
绑定到全局对象(或 undefined
在严格模式下)。另一方面,当您将函数作为对象上的方法调用时(即直接在 Ember.Object
上调用它)会将 this
绑定到该对象。
这就是 this
在 create
中未定义的原因,因此 this._initProperties
不是一个函数。
这是一个正在发生的事情的演示:
// an object with a method
var obj = {
getThis: function() {
return this;
}
};
// an extracted reference to the method
var extractedGetThis = obj.getThis;
// this binds to the object
console.log(
obj.getThis() === obj // true
);
// this binds globally
console.log(
extractedGetThis() === window // true
);