我在 ember 控制器中定义了一个动作,它调用了作为控制器一部分的 2 个单独的函数。我想在单元测试中模拟这些函数,以确认操作方法是否调用了正确的函数。
我的控制器如下所示:
export default Ember.Controller.extend({
functionA() {
return;
},
functionB() {
return;
},
actions: {
actionMethod(param) {
if(param) {
return this.functionA();
}
else {
return this.functionB();
}
}
}
});
实际上,控制器可以工作,但是在单元测试中,functionA 和 functionB 都是未定义的。我试图登录this
到控制台,但找不到 functionA 和 functionB 的位置,所以我无法正确模拟它们。我希望它们位于动作旁边的对象的顶层,但我只找到_actions
了actionMethod
正确定义的。
我的单元测试如下所示
const functionA = function() { return; }
const functionB = function() { return; }
test('it can do something', function(assert) {
let controller = this.subject();
// I don't want the real functions to run
controller.set('functionA', functionA);
controller.set('functionB', functionB);
controller.send('actionMethod', '');
// raises TypeError: this.functionA is not a function
// this doesn't work etiher
// controller.functionB = functionB;
// controller.functionA = functionA;
// controller.actions.actionMethod();
}
有人对我如何在测试环境中替换这些功能有任何想法吗?或者,是否有更好的方法来测试此功能或设置我的控制器?
- 编辑错字:this.subject to this.subject()