我正在尝试模拟一个EmberJS适配器,该适配器具有执行POST请求的功能。我的测试如下所示:
test('should post something', async function (assert) {
let controller = this.owner.lookup('path/to/ach-deposit');
controller.setProperties({
...,
store: {
adapterFor: () => {
return {postAchDeposit: sinon.spy()}
}
}
})
await controller.actions.postAchDepositHandler.call(controller);
assert.ok(controller.store.adapterFor.call().postAchDeposit.called);
})
此操作失败。单步执行调用 postAchDeposit 的代码不会引发任何错误。如果我sinon.spy()
更改为sinon.stub().return("Hi")
它将返回Hi
但是无论出于何种原因,当我尝试查看它是否已被调用时,它都会返回 false。
在调试器中调用 postAchDeposit,如果我使用this.get('store').adapterFor('funding/ach-deposit').postAchDeposit.called
检查它仍然返回 false。
我错过了什么?
每次调用adapterFor
时sinon.spy()
都会再次调用,因此会创建一个新的间谍。
所以基本要素:
controller.store.adapterFor().postAchDeposit !== controller.store.adapterFor().postAchDeposit
您可能应该首先创建存根,然后始终传递引用:
const postAchDeposit = sinon.spy();
controller.setProperties({
...,
store: {
adapterFor: () => {
return {postAchDeposit}
}
}
});
await controller.actions.postAchDepositHandler.call(controller);
assert.ok(postAchDeposit.called);