3

如何测试组件中是否调用了某个操作?

有多种触发动作的方法,例如单击按钮。现在我想测试单击该按钮时调用的操作是否被实际调用。什么东西之类expect.functionName.to.be.called的。

我有以下代码

test('it closes the create dialog when close btn is clicked', function(assert) {
  this.render(hbs`{{group-create cancelCreateAction="cancelAction"}}`)

  this.$('button.btn--primary').click()
  expect('myAction').to.be.called?
})

所以我只是想知道我能在那里做什么?

4

2 回答 2

1

那么你的行为做了一些我们不知道的事情。但这是我编写的一个小测试,用于检查一些 DOM 元素和当前路线。如果你不告诉我们你的行为是做什么的,很难说清楚。

click('.someSavingButton');

   andThen(function() {
     assert.equal(currentRouteName(), 'index');
     assert.equal(find('.something-new-in-the-dom').length, 1, "New item in HTML");
于 2015-11-08T07:46:05.253 回答
1

我偶然发现了这个问题,同时也在寻找一种方法来测试集成测试中的冒泡操作(而不是关闭操作)。也许您已经找到了解决方案,但我会回答让下一个人比​​我更早找到它。

测试一个动作是否被调用的惯用方法是编写一个模拟函数并断言它将被调用。在您的示例中 - 在关闭操作之前 - 编写这种测试的方法如下:

test('it closes the create dialog when close btn is clicked', function(assert) {
  // make sure our assertion is actually tested
  assert.expect(1);

  // bind the action in the current test
  this.on('cancelAction', (actual) => {
    let expected = { whatever: 'you have expected' };
    assert.deepEquals(actual, expected);

    // or maybe just an assert.ok(true) - but I am not sure if this is "good" style
  });

  this.render(hbs`{{group-create cancelCreateAction="cancelAction"}}`)

  this.$('button.btn--primary').click()
  expect('myAction').to.be.called?
});

如今,使用闭包动作范例,绑定模拟函数的正确方法是

// bind the action in the current test
this.set('cancelAction', (actual) => {
  let expected = { whatever: 'you have expected' };
  assert.deepEquals(actual, expected);
});

this.render(hbs`{{group-create cancelCreateAction=(action cancelAction)}}`)
于 2016-02-12T06:57:59.090 回答