使用 Jasmine 进行单元测试,expect().toBeVisible()

Unit testing with Jasmine, expect().toBeVisible()

本文关键字:expect toBeVisible 单元测试 Jasmine 使用      更新时间:2023-09-26

我需要在执行点击事件后检测 CSS 显示值。我确定这是用户错误,但我无法让 Jasmine.js 报告单击后元素可见性是否发生了变化。

这是代码笔链接:http://codepen.io/nicholasabrams/pen/dPYoJr

这是代码本身:(这显然不是使用它的实际情况,但是它是一个非常简化的版本,可以重现我当前遇到的问题)

$('#button1').on('click', function(){
  $(this).append('<button id="#button2">!!!!!</button>');
});

这是测试本身:

describe("button#1 click test", function(){
   beforeEach(function(){
     $('#button1').click();
   });
   it("Should inject a new button with #button2 as the id", function(){
      expect($('#button2')).toBeVisible();
   });
});
首先 -

在你的笔中,你应该为 jasmine-jquery 添加外部脚本。接下来,按钮中的按钮,根据W3C Phrasing content, but there must be no interactive content descendant. Jasmine是非法的,请尝试附加到父级:

$('#button1').on('click', function(){
    $(this).parent().append('<button id="#button2">!!!!!</button>');
});

工作笔 http://codepen.io/anon/pen/zxvGja?editors=101