如何假冒"点击继续"在保存方法的中间进行测试?

时间:2016-06-30 13:31:46

标签: angularjs unit-testing typescript jasmine

以下代码位于save方法的主体中,并且烤面包机的clickHandler完成了保存过程。所以,我需要做的是在我的保存方法中点击。我知道如何检索元素并触发点击,但我不知道如何正确地执行此特定测试。

return promise.then(() => {

    // warn user
    if (!mustPromptUser) {
        ctrl.toasterService.warning({
            body: "A message",
            timeout: 0,
            showCloseButton: true,
            clickHandler: onSaveFunc, <--- must assert this happened
            bodyOutputType: "trustedHtml"
        });
    } else {
        onSaveFunc();
    }
}).catch(() => {
    // some code
});

以下是测试:

    it("Can I get this damned test working", () => {
        var controller = initializeController(model) as MyController;
        controller.form = {
            $pristine: true,
            $setPristine: null
        } as ng.IFormController;
        spyOn(controller, "onSave");
        spyOn(controller.form, "$setPristine");
        spyOn(service, "create");
        controller.save();
        $timeout.flush();

        expect(service.create).toHaveBeenCalled();
        expect(toaster.warning).toHaveBeenCalled();
        // This is the assertion that's failing because the user has to click to call this method in the toaster clickHandler. 
        expect(controller.onSave).toHaveBeenCalled();
        expect(controller.form.$pristine).toBeTruthy();

    });

0 个答案:

没有答案