在测试Ember.js(ember-cli)应用程序时,在拆解时获取“Ap​​p is not defined”

时间:2014-06-24 09:07:35

标签: javascript ember.js ember-cli

我试图测试一个简单的ember-cli应用程序(关注@bcardarella Dockyard教程:http://reefpoints.dockyard.com/2014/05/09/building-an-ember-app-with-rails-part-3.html

这是我的测试:

import startApp from 'wallet2/tests/helpers/start-app';
module('Integration - Landing Page', {
  setup: function() {
    var App;
    return App = startApp();
  },
  teardown: function() {
    return Ember.run(App, "destroy");
  }
});

test('Should ask me to log in', function() {
  return visit('/').then(function() {
    equal(find('h1#title').text(), 'Please login');
    return equal(find('form').length, 1);
  });
});

测试有效,但我得到

3. Teardown failed on Should ask me to log in: App is not defined
Source:     
    at Object.module.teardown (wallet2/tests/integration/landing-page-test.js:13:26)
    at Object.Test.teardown (http://localhost:4200/assets/qunit.js:228:35)
    at http://localhost:4200/assets/qunit.js:364:10
    at process (http://localhost:4200/assets/qunit.js:1453:24)
    at http://localhost:4200/assets/qunit.js:479:5

这是我的start-app.js

var Application = require('wallet2/app')['default'];
var Router = require('wallet2/router')['default'];

export default function startApp(attrs) {
  var App;

  var attributes = Ember.merge({
    // useful Test defaults
    rootElement: '#ember-testing',
    LOG_ACTIVE_GENERATION:false,
    LOG_VIEW_LOOKUPS: false
  }, attrs); // but you can override;

  Router.reopen({
    location: 'none'
  });

  Ember.run(function(){
    App = Application.create(attributes);
    App.setupForTesting();
    App.injectTestHelpers();
  });

  App.reset(); // this shouldn't be needed, i want to be able to "start an app at a specific URL"

  return App;
}

这让我从昨天起就疯了。谢谢你的帮助!

1 个答案:

答案 0 :(得分:4)

App仅在setup方法中定义。 使用以下内容更新模块部件:

    import startApp from '../../helpers/start-app'; // change this due to your folder
    var App;
    module('Integration - Landing Page', {
      setup: function() {
        App = startApp();
      },
      teardown: function() {
        Ember.run(App, "destroy");
      }
    });