如何对调用 DialogController 的方法进行单元测试?我想测试this.controller.ok()
被称为。
ReprocessLotDialog.js
@inject(DialogController)
export class ReprocessLotDialog {
constructor(dialogController) {
this.controller = dialogController;
}
commitReprocessingLot() {
this.controller.ok({
reprocessLot: this.model
});
}
commitIncompleteBags(){
... do stuff ....
this.commitReprocessingLot();
}
}
myDialog.spec.js
我已经尝试过了,但无法让它工作
describe("The ReprocessLotDialog module", ()=> {
let sut;
beforeEach(()=> {
var container = new Container().makeGlobal();
container.registerInstance(DialogController,controller => new DialogController());
sut = container.get(ReprocessLotDialog);
});
it("commitReprocessingLot() should call controller.ok", (done)=> {
spyOn(sut, "controller.ok");
sut.commitIncompleteBags();
expect(sut.controller.ok).toHaveBeenCalled();
done();
});
});
测试失败TypeError: 'undefined' is not a function (evaluating 'this.controller.ok({ reprocessLot: this.model })')
据我了解,我将DialogController
通过 DI 传递到DialogControllercontainer
并将container.get(ReprocessLotDialog)
DialogController 注入到ctor中。
我也试过
container.registerInstance(DialogController,{"dialogController":DialogController});
,但这也不起作用。
非常感谢