EmberJS + EmberCLI单元测试助手功能未定义

时间:2014-10-25 18:28:52

标签: javascript unit-testing ember.js ember-cli

我正在尝试使用Ember CLI让测试助手工作,但我总是得到[functionName] is undefined。根据{{​​3}}:

,这是我的测试帮助
`import Ember from "ember"`

acceptanceHelpers = ->
    Ember.Test.registerAsyncHelper("performLogin", (app) ->
        visit "/login"

        fillIn("#username", "sampleusername")
        fillIn("#password", "samplepassword")

        click(".form-actions button:first")
        wait()
    )

`export default acceptanceHelpers`

我在哪里使用它: login-test.coffee

`import Ember from "ember"`
`import startApp from "../helpers/start-app"`
`import acceptanceHelpers from "../helpers/acceptance-helpers"`

App = null

module "Acceptance: Login",
    setup: ->
        App = startApp()

    teardown: ->
        Ember.run App, "destroy"

test "User is able to log in / transition to dashboard", ->
    performLogin()
    andThen ->
        equal(currentRouteName(), "dashboard")

但我明白了:

  

ReferenceError:未定义performLogin       在Object.module.setup(unhost / tests / acceptance / login-test.js:15:16)       at Object.Test.setup(http:// localhost:4200 / assets / test-support.js:1063:31)       在http:// localhost:4200 / assets / test-support.js:1168:10       在过程中(http {// localhost:4200 / assets / test-support.js:887:24)       在http:/ /localhost:4200 / assets / test-support.js:476:5

如何配置验收测试以在Ember CLI中正确使用已定义的测试助手?

2 个答案:

答案 0 :(得分:3)

这里有几个选项:

如果要导入每个测试文件,可以使用普通的帮助函数:

//acceptance-helpers.js
export function performLogin() {
  visit "/login"

  fillIn("#username", "sampleusername")
  fillIn("#password", "samplepassword")

  click(".form-actions button:first")
  //  wait()  <--- This wait is unnecessary, 'click' will cause a wait  
}

然后在测试中:

//login-test.js
import Ember from 'ember';
import startApp from '../helpers/start-app';
import { performLogin } from '../helpers/acceptance-helpers';

var App;
module("Acceptance: Login", {
  setup: function(){
    App = startApp();
  },
  teardown: function(){
    Ember.run(App, 'destroy');
  }
});

test("User is able to log in / transition to dashboard", function(){
  performLogin();
  andThen(function(){
    equal(currentRouteName(), "dashboard");
  });
});

制作和使用已登记的助手:

//Easiest is to put in test-helper.js ... it must be evaled before injectTestHelpers is called in   startApp
Ember.Test.registerAsyncHelper( 'performLogin', function ( app ) {
  visit "/login"

  fillIn("#username", "sampleusername")
  fillIn("#password", "samplepassword")

  click(".form-actions button:first")
  // wait()  <--- wait is again unnecessary
});

然后在测试中:

//login-test.js
import Ember from 'ember';
import startApp from '../helpers/start-app';
//No need to import anything to get performLogin, it was registered 

var App;
module("Acceptance: Login", {
  setup: function(){
    App = startApp();
  },
  teardown: function(){
    Ember.run(App, 'destroy');
  }
});

test("User is able to log in / transition to dashboard", function(){
  performLogin();
  andThen(function(){
    equal(currentRouteName(), "dashboard");
  });
});

答案 1 :(得分:1)

这很可能是jshint失败,您需要将performLogin添加到tests/.jshintrcpredef数组:

"predef": [
    "document",
    "window",
    "performLogin", // like this
    ...