0

这是我的三个测试的一个例子......

  describe('Enabled button (no disabled attribute)', function () {
    var el, clicked = false;

    beforeEach(function (done) {
      // Create the tag
      el = document.createElement('rui-button')
      el.innerHTML = 'Test Button';
      document.body.appendChild(el)

      el.addEventListener('click', function () {
        clicked = true;
        done();
      });

      // Mount the tag
      tag = riot.mount('rui-button')[0]
      expect(tag).toBeDefined();
      expect(tag.isMounted).toBe(true);
      tag.update();

      el.childNodes[0].click();
    });

    it('should be disabled', function () {
      expect(el.getAttribute('data-disabled')).toEqual('false');
      expect(clicked).toEqual(true);

      var style = window.getComputedStyle(el.childNodes[0]);
      expect(style.cursor).toEqual('pointer');
      expect(style.opacity).toEqual('1')
    });
  });

  describe('Enabled button (disabled="{ false }")', function () {
    var el, clicked = false;

    beforeEach(function (done) {
      // Create the tag
      el = document.createElement('rui-button')
      el.innerHTML = 'Test Button';
      el.setAttribute('disabled', '{ false }');
      document.body.appendChild(el)

      el.addEventListener('click', function () {
        clicked = true;
        done();
      });

      // Mount the tag
      tag = riot.mount('rui-button')[0]
      expect(tag).toBeDefined();
      expect(tag.isMounted).toBe(true);
      tag.update();

      el.childNodes[0].click();
    });

    it('should be disabled', function () {
      expect(el.getAttribute('data-disabled')).toEqual('false');
      expect(clicked).toEqual(true);

      var style = window.getComputedStyle(el.childNodes[0]);
      expect(style.cursor).toEqual('pointer');
      expect(style.opacity).toEqual('1')
    });
  });

  describe('Disabled button (disabled)', function () {
    var el, clicked = false;

    beforeEach(function (done) {
      // Create the tag
      el = document.createElement('rui-button')
      el.innerHTML = 'Test Button';
      el.setAttribute('disabled', '');
      document.body.appendChild(el)

      el.addEventListener('click', function () {
        clicked = true;
        done();
      });

      // Mount the tag
      tag = riot.mount('rui-button')[0]
      expect(tag).toBeDefined();
      expect(tag.isMounted).toBe(true);
      tag.update();

      el.childNodes[0].click();
    });

    it('should be disabled', function () {
      expect(el.getAttribute('data-disabled')).toEqual('true');
      expect(clicked).toEqual(false);

      var style = window.getComputedStyle(el.childNodes[0]);
      expect(style.cursor).toEqual('not-allowed');
      expect(style.opacity).not.toEqual('1')
    });
  });

如您所见,每个测试由 a describe、 abeforeEachit. 这对我来说很多,当测试失败时,我会收到一些无用的超时错误。

这是我在测试工作时得到的:

Enabled button (no disabled attribute)
  ✓ should be enabled
Enabled button (disabled="{ false }")
  ✓ should be enabled
Disabled button (disabled)
  ✗ should be disabled
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

无论如何它都失败了,测试通过但超时导致失败。我正在尝试测试 click 事件没有发生。它也是 6 行,每个测试两行。我真的更希望有这个:

✓ Enabled button (no disabled attribute)
✓ Enabled button (disabled="{ false }")
✓ Disabled button (disabled)

我需要进行负面测试,测试缺少点击。

当测试确实失败时,我得到了这个,同样的超时错误以及有效的失败原因。

  ✗ should be enabled
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

Expected false to equal true.
    at Object.<anonymous> (ui-button.spec.js:89:23)

有什么方法可以测试缺少事件吗?还有其他更好的方法,我可以编写我的规范文件吗?我在这里只列出了 3 个测试,但我还有更多。

4

1 回答 1

0

你有两个选择。

  1. 更简单的方法是在没有beforeEach点击处理程序执行done()回调的地方添加否定测试。您显然不能单击禁用的 HTML 元素。

2.beforeEach像这样改变你的例子(关键是done()在某个时候执行回调beforeEach):

beforeEach(function (done) {
  // Create the tag
  el = document.createElement('rui-button')
  el.innerHTML = 'Test Button';
  el.setAttribute('disabled', '');
  document.body.appendChild(el)

  el.addEventListener('click', function () {
    clicked = true;
    done();
  });

  // Mount the tag
  tag = riot.mount('rui-button')[0]
  expect(tag).toBeDefined();
  expect(tag.isMounted).toBe(true);
  tag.update();

  if (el.getAttribute('data-disabled')) {
    done();
  } else {
    el.childNodes[0].click();      
  }
});
于 2016-10-27T08:21:50.663 回答