unit-testing - Emberjs 测试组件的 Action 委托(delegate)

标签 unit-testing testing ember.js sinon

我正在尝试测试一个使用 Action 委托(delegate)的简单组件。我想测试某个 Action 是否发送到组件的 actionDelegate。我正在考虑使用 Sinon 来检查是否正在发送操作,但我不知道如何复制 Ember 用来将其操作发送到其委托(delegate)/目标的结构。

当用户点击按钮时,我的 sinon“spy delegate”对象会采用什么结构来检查组件是否正在使用“send”委托(delegate)事件?

我已经在 http://jsfiddle.net/L3M4T/ 创建了一个我想测试的例子但它周围没有测试工具(只是为了一个简单的 js fiddle ,在组件周围放置一个测试工具是一项艰巨的工作——事实上,将这个组件变成我的形状是一项相当大的工作想解释这个问题)。

这是我的组件:

App.AppProfileComponent = Ember.Component.extend({
    actionDelegate: null,
    actions: {
        hello: function(person) {
            if(!Ember.isEmpty(this.get('actionDelegate'))) {
                this.get('actionDelegate').send('hello', person);
            }
        }
      }
});

我最初的尝试没有成功,只是简单地编写了一个包含这个片段的测试(使用 sinon 和 qunit):

visit("/").click("button").then(function() {
  equal(actionDelegateSpy.called, true, "Action delegate should be called when button pressed");
});

我认为很明显为什么那个不起作用,但是由于我尝试了以下方法,它也不起作用:

var actionDelegateSpy = sinon.spy();
var actionDelegate = Ember.ObjectController.extend({
  actions: {
    "hello" : actionDelegateSpy
  }
}).create();

然后通过将上面定义的actionDelegate作为组件上的actionDelegate传入进行测试。

最佳答案

我解决了我自己的问题......愚蠢的我:

  test("delegates its hello action to actionDelegate", function() {
    var actionDelegateSpy;

    Ember.run(function() {
      actionDelegateSpy = sinon.spy();
      var actionDelegate = Ember.ObjectController.extend({
        actions: {
          "hello" : actionDelegateSpy
        }
      }).create();
      controller.set('actionDelegate', actionDelegate);      
    });

    visit("/").click("button")
    .then(function() { 
      equal(actionDelegateSpy.called, true, "Action delegate should be called when hello button pressed");
    });
  });

关于unit-testing - Emberjs 测试组件的 Action 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22677403/

相关文章:

unit-testing - 如何用reflect测试方法?

java - TestNG中@DataProvider和@Parameters的区别

python - 我如何在 python 中实现此功能?

javascript - 将 html 元素的值作为 ember 中的参数传递

ios - 未发现 Xcode swift 测试

c# - 调用回调时 Moq 回调值不会改变

unit-testing - 如何制作 "Corrupt"文件

javascript - 使用 Jasmine spies 测试函数的调用

node.js - 通过 cmd 创建 ember 应用程序时出现以下错误

ember.js - #Ember.js 或 {{@index}} 中的每个迭代编号