您如何使用 jasmine 在 aurelia-dialog 中调用 ok() 等方法进行单元测试?
How do you unit test that methods such as ok() were called in aurelia-dialog using jasmine?
如何对调用 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 })')
据我了解,我正在通过 DI 将 DialogController
传递到 container
中,然后 container.get(ReprocessLotDialog)
将 DialogController 注入到 ctor 中。
我也试过了
container.registerInstance(DialogController,{"dialogController":DialogController});
但这也不起作用。
非常感谢
您的单元测试不应使用 DI 容器。您只需新建一个 ReprocessLotDialog
的实例并传入您创建的 DialogController
的模拟。如果您使用的是 Jasmine,它看起来像这样:
describe("The ReprocessLotDialog module", ()=> {
it("commitReprocessingLot() should call controller.ok", ()=> {
let dialogController = {
ok: (arg) = { }
};
spyOn(dialogController, "ok");
let sut = new ReprocessLotDialog(dialogController);
sut.commitIncompleteBags();
expect(dialogController.ok).toHaveBeenCalled();
});
});
您可能还想考虑是否进行测试,如果您不应该只测试 commitReprocessingLot
是否已被调用,而不是检查它是否调用了它唯一做的事情的方法(至少在您的示例中).
describe("The ReprocessLotDialog module", ()=> {
it("commitReprocessingLot() should call controller.ok", ()=> {
let dialogController = {
ok: (arg) = { }
};
let sut = new ReprocessLotDialog(dialogController);
spyOn(sut, "commitReprocessingLot");
sut.commitIncompleteBags();
expect(su.commitReprocessingLot).toHaveBeenCalled();
});
});
你绝对可以关闭控制器或者你可以像这样监视实例方法 -
describe("The ReprocessLotDialog module", ()=> {
let container;
let sut;
let controller;
beforeEach(()=> {
container = new Container().makeGlobal();
controller = container.get(DialogController);
sut = container.get(ReprocessLotDialog);
});
it("commitReprocessingLot() should call controller.ok", (done)=> {
spyOn(controller, 'ok');
sut.commitIncompleteBags();
expect(controller.ok).toHaveBeenCalled();
done();
});
});
基本上,您应该能够在控制器的容器中创建一个实例,该实例将被传入,您可以直接在该实例上监视该方法。
如何对调用 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 })')
据我了解,我正在通过 DI 将 DialogController
传递到 container
中,然后 container.get(ReprocessLotDialog)
将 DialogController 注入到 ctor 中。
我也试过了
container.registerInstance(DialogController,{"dialogController":DialogController});
但这也不起作用。
非常感谢
您的单元测试不应使用 DI 容器。您只需新建一个 ReprocessLotDialog
的实例并传入您创建的 DialogController
的模拟。如果您使用的是 Jasmine,它看起来像这样:
describe("The ReprocessLotDialog module", ()=> {
it("commitReprocessingLot() should call controller.ok", ()=> {
let dialogController = {
ok: (arg) = { }
};
spyOn(dialogController, "ok");
let sut = new ReprocessLotDialog(dialogController);
sut.commitIncompleteBags();
expect(dialogController.ok).toHaveBeenCalled();
});
});
您可能还想考虑是否进行测试,如果您不应该只测试 commitReprocessingLot
是否已被调用,而不是检查它是否调用了它唯一做的事情的方法(至少在您的示例中).
describe("The ReprocessLotDialog module", ()=> {
it("commitReprocessingLot() should call controller.ok", ()=> {
let dialogController = {
ok: (arg) = { }
};
let sut = new ReprocessLotDialog(dialogController);
spyOn(sut, "commitReprocessingLot");
sut.commitIncompleteBags();
expect(su.commitReprocessingLot).toHaveBeenCalled();
});
});
你绝对可以关闭控制器或者你可以像这样监视实例方法 -
describe("The ReprocessLotDialog module", ()=> {
let container;
let sut;
let controller;
beforeEach(()=> {
container = new Container().makeGlobal();
controller = container.get(DialogController);
sut = container.get(ReprocessLotDialog);
});
it("commitReprocessingLot() should call controller.ok", (done)=> {
spyOn(controller, 'ok');
sut.commitIncompleteBags();
expect(controller.ok).toHaveBeenCalled();
done();
});
});
基本上,您应该能够在控制器的容器中创建一个实例,该实例将被传入,您可以直接在该实例上监视该方法。