繁体   English   中英

茉莉花测试未检测到已单击按钮

[英]Jasmine test is not detecting that button has been clicked

我编写了一个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>

谢谢

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

谢谢...张贴后不久我就怀疑了。 从原理上讲,我认为其他答案类似。 这是因为我需要获取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);
            });
        });
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM