【问题标题】:Nesting ember acceptance test helpers in "andThen"在“andThen”中嵌套 ember 验收测试助手
【发布时间】:2016-09-23 15:56:25
【问题描述】:

我总是这样写 ember 测试:

test('should add new post', function(assert) {
  visit('/posts/new');

  fillIn('input.title', 'My new post');
  click('button.submit');

  andThen(() => {
    assert.equal(find('ul.posts li:first').text(), 'My new post')
  });

  click('button.edit');
  fillIn('input.title', 'My edited post');
  click('button.submit');

  andThen(() => {
    assert.equal(find('ul.posts li:first').text(), 'My edited post')
  });
});

但我也看到像这样编写“嵌套”样式的测试:

test('should add new post', function(assert) {
  visit('/posts/new');

  fillIn('input.title', 'My new post');
  click('button.submit');

  andThen(() => {
    assert.equal(find('ul.posts li:first').text(), 'My new post')

    click('button.edit');
    fillIn('input.title', 'My edited post');
    click('button.submit');

    andThen(() => {
      assert.equal(find('ul.posts li:first').text(), 'My edited post')
    });
  });
});

一种方法比另一种更好还是正确?第一种样式可能是竞争条件的来源吗?

我在 github 上查找了一些开源 ember 应用程序,发现它们中的大多数都按照我的方式进行操作:

https://github.com/cowbell/splittypie/blob/master/tests/acceptance/event-test.js

这是一个嵌套的例子:

https://github.com/HospitalRun/hospitalrun-frontend/blob/master/tests/acceptance/admin-test.js

【问题讨论】:

  • 没有理由将async helpers 包裹在andThen 中。
  • 这是我的理解,为什么会有人这样做呢?我能想象的唯一原因是,如果你包装它,你可以在异步助手上设置断点,否则你不能。

标签: ember.js ember-cli acceptance-testing


【解决方案1】:

你会嵌套的一些原因是

  • 如果随后的测试依赖于初始测试
  • 如果需要特定的序列

我个人更喜欢嵌套和assert.async()

例如:

test('should add new post', function(assert) {
  var done = assert.async();
  visit('/posts/new');

  fillIn('input.title', 'My new post');
  click('button.submit');

  andThen(() => {
    assert.equal(find('ul.posts li:first').text(), 'My new post')

    click('button.edit');
    fillIn('input.title', 'My edited post');
    click('button.submit');

    andThen(() => {
      assert.equal(find('ul.posts li:first').text(), 'My edited post')
      done();
    });
  });
});

根据我的个人经验,andThen 并不总是按预期发生,尤其是如果您的开发代码中有计时器,因此使用 done 可以确保我的所有测试都成功并按顺序完成

【讨论】:

    【解决方案2】:

    没有充分的理由使用 andThen 嵌套,尤其是现在您可以在 Ember 中使用 async/await。第二个例子可以重写:

    test('should add new post', async function(assert) {
      await visit('/posts/new');
    
      fillIn('input.title', 'My new post');
      await click('button.submit');
    
      assert.equal(find('ul.posts li:first').text(), 'My new post')
    
      await click('button.edit');
      fillIn('input.title', 'My edited post');
      await click('button.submit');
    
      assert.equal(find('ul.posts li:first').text(), 'My edited post')
    });
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多