Jasmine测试未检测到已单击该按钮

时间:2015-10-09 10:22:08

标签: html angularjs jasmine

我编写了一个Jasmine测试,点击列表中的第一条错误消息并将其关闭。然后检查错误数量是否已减少到预期数量:

it('should close the error if the errors close button is clicked', function() {
    element.all(by.repeater('error in errorList')).then(function(errorList) {
        errorList[0].element(by.id('error0')).then(function(error0) {
            error0.click();
            var arrayLength = errorList.length;
            expect(arrayLength).toEqual(1);
        });
    });
});

当我运行此消息时,我收到消息Expected 2 to equal 1。 2是测试开始时错误数组的长度。如果我手动重新创建此项,则单击error0内的任何位置时肯定会关闭错误消息。单击此操作是否可能需要一些时间,并且在expect语句运行时尚未注册?

以下是HTML的相关部分:

<a class="po-cross-link" href="" ng-click="closeError(error)" id="{{'error'+$index}}">
                    <img class="po-cross" src="\assets\black-cross.png" alt="close">
                </a>

由于

2 个答案:

答案 0 :(得分:0)

我猜您正在更改点击处理程序中的模型,但您希望DOM元素能够立即更改。 Angular需要$digest个周期来根据模型更新DOM,因此我建议您在点击后运行scope.$digest()。如果您的测试中没有范围,您也可以使用$rootScope

答案 1 :(得分:0)

谢谢......我在发帖后不久就发现了这件事。我认为原则上与其他答案类似。这是因为我需要获得最新版本的DOM。我在第二个异步函数中添加如下:

it('should close the error if the errors close button is clicked', function() {
    element.all(by.repeater('error in errorList')).then(function(errorList) {
        errorList[0].element(by.id('error0')).then(function(error0) {
            error0.click();
            element.all(by.repeater('error in errorList')).then(function(errorListNew) {
                var arrayLength = errorListNew.length;
                expect(arrayLength).toEqual(1);
            });
        });
    });
});