コントローラーの一部である 2 つの別個の関数を呼び出す、ember コントローラーで定義された単一のアクションがあります。アクションメソッドが正しい関数を呼び出したかどうかを確認するために、単体テストでこれらの関数をモックアウトしたいと思います。
私のコントローラーは次のようになります。
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 から this.subject()