如何使用Intern对非AMD JavaScript文件进行单元测试?

时间:2015-02-15 02:37:30

标签: intern

我有一个类似于Snake类的非AMD JavaScript文件:

// ./www/js/Snake.js
function Snake(initPos) {
  this.pos = initPos;
}

// I want to unit test this method.
Snake.proptotype.move = function(dir) {
  // Do things.
};

我写了一个Intern模块来测试它:

// ./test/Snake.js
define([
  'intern!object',
  'intern/chai!assert',
  "../www/js/Snake"
], function (registerSuite, assert, Snake) {
  registerSuite({

  name: 'Snake',

  move: function () {
    // Intern complains that this object is not a function.
    var snake = new Snake([
      {x: 0, y: 0}, 
      {x: 1, y: 0},
      {x: 2, y: 0}
    ]);

    snake.move("right");

    ...

如何让实习生认出Snake课程?

1 个答案:

答案 0 :(得分:3)

我发现了问题所在。在实习配置文件中,我需要设置loader packages选项,如下所示:

// ./tests/intern.js
define({
  ...
  loader: {
    packages: [ { name: 'app', location: 'www/js' } ]
  },
  ...

然后在我的测试模块中,我需要导入Snake.js文件:

// ./tests/Snake.js
define([
  'intern!object',
  'intern/chai!assert',
  "intern/order!app/Snake.js"
], function (registerSuite, assert) {
  registerSuite({
  name: 'Snake',

  move: function () {
    var snake = new Snake([
      {x: 0, y: 0}, 
      {x: 1, y: 0},
      {x: 2, y: 0}
    ]);
    ...

现在它没有抱怨Snake未定义。