如何通过茶包使用余烬测试(Quit)为余烬集成测试创建动态夹具


  • 我已经设置了相当棒的茶包https://github.com/modeset/teabag在我的ember应用程序
  • 我正在遵循Erik Bryn在演示文稿:http://www.youtube.com/watch?v=nO1hxT9GBTs使用代码在https://github.com/ebryn/bloggr-client-rails作为模板
  • 我切换到余烬夹具适配器进行测试

我可以很好地使用静态夹具,我在夹具文件中定义了它,如下所示:

var user = {
    id: 1,
    email: "test@testing.com",
    firstName: "Han",
    lastName: "Solo"
};
var userArray = [];
userArray.push(user);
App.User.FIXTURES = userArray; 

但是。。。。如果我想从我现有的Rails种子数据中动态地提取fixture数据。(我将文件重命名为user_fixture.js.erb(,然后执行:

<% @user = User.find_by_email('test@testing.com') %>
var currentUser = <%= UserSerializer.new(@user, :root => false).to_json %>; 
var userArray = [];
userArray.push(user);
App.User.FIXTURES = userArray; 

我的测试失败了。我可以看到数据没有填充到ember应用程序中,但如果我填充了:

App.User.Fixtures

从控制台中,我可以看到该对象按预期存在。我不知道为什么会这样,有人有什么建议吗?

为了完整起见,以下是我在测试中设置Ember的方式(与Erik Bryn的例子相同(:

module("Home Routes:", {
  setup: function() {
    Ember.run(App, App.advanceReadiness);
  },
  teardown: function() {
    App.reset();
  }
});

下面是test_helper.js:的内容

//= require application
//= require_tree .
//= require_self
document.write('<div id="ember-testing-container"><div id="ember-testing"></div></div>');
document.write('<style>#ember-testing-container { position: absolute; background: white; bottom: 45px; left: 45px; width: 640px; height: 384px; overflow: auto; z-index: 9999; border: 1px solid #ccc; } #ember-testing { zoom: 50%; }</style>');
App.rootElement = '#ember-testing';
App.setupForTesting();
App.injectTestHelpers();
function exists(selector) {
    return !!find(selector).length;
};

更新:我已经发现,这个问题的一个(唯一的问题是(是Active Model Serializer正在创建下划线键名称,而Ember正在期待Camalized版本。

好吧,经过一番折腾,我终于开始工作了。正如我在上面的更新中提到的,问题是ember需要camalized属性,而Active Model Serializers创建Dashalized属性。

module MyUtilities
  class Base
    # Convert keys from ruby object to camelized strings. e.g: :hello_there_world becomes "helloThereWorld"
    def self.camelize_keys(object)
      r = {}
      object.to_hash.each do |k,v|
        r[k.to_s.camelize(:lower)] = v
      end
      r
    end
  end
end

<% 
  user = User.find_by_email('test@testing.com') 
  user_hash = UserSerializer.new(user, :root => false).as_json
  @user = MyUtilities::Base.camelize_keys(user_hash).to_json
%>
var user = <%= @user %>; 
App.User.FIXTURES = [user]; 

最新更新