How do I go about unit testing a method that calls the DialogController? I want to test that this.controller.ok() was called.
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 I've tried this but can't get it to work
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();
});
});
The test fails with TypeError: 'undefined' is not a function (evaluating 'this.controller.ok({ reprocessLot: this.model })')
As far as I understand it I'm passing the DialogController through DI into the container and container.get(ReprocessLotDialog) injects the DialogController into the ctor.
I've also tried
container.registerInstance(DialogController,{"dialogController":DialogController}); but that doesn't work either.
Many thanks
Your unit tests shouldn't be utilizing the DI container. You simply new up an instance of
ReprocessLotDialogand pass in a mock ofDialogControllerthat you created. If you're using Jasmine, it would look something like this:You might also want to consider if testing if you shouldn't just be testing if
commitReprocessingLothas been called instead of checking that it calls the method that's the only thing it does (at least in your example).