如何在茉莉花测试中使用npm模块?

时间:2016-03-23 08:00:34

标签: html node.js jasmine

我想要一个npm模块(这里的一个简单例子是array-equal

这是我的茉莉花测试规范:

describe('BinoxxoController', function() {
beforeEach(angular.mock.module('binoxxoapp'));

beforeEach(inject(function(_$controller_) {
    $controller = _$controller_;

}));

describe('$scope.lineIsValid', function() {
    var $scope, controller;

    beforeEach(function() {
        $scope = {};

        var controller = $controller('BinoxxoController', { $scope: $scope});

    })


    it('equals array', function() {
        var array0 = [1, 2, 3];
        var array1 = [1, 2, 3];
        expect(equal(array0, array1)).toBe(true);

    });


});

});

index.html(testrunner)

<!doctype html>
<html>
  <head>
    <title>Jasmine Spec Runner</title>
    <link rel="stylesheet" href="../../../node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
  </head>
  <body>
    <script src="../../../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
    <script src="../../../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
    <script src="../../../node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
    <script src="../../../node_modules/angular/angular.js"></script>
    <script src="../../../node_modules/angular-mocks/angular-mocks.js"></script>

    <!-- include source files here... -->
    <script src="../../../static/js/app.js"></script>
    <script src="../../../node_modules/array-equal/index.js"></script>

    <!-- include spec files here... -->
    <script src="spec/controllerSpec.js"></script>
  </body>
</html>

我包含了该模块,但我仍然收到错误ReferenceError: equal is not defined

如果有帮助,这是数组相等的模块:

module.exports = function equal(arr1, arr2) {
  var length = arr1.length
  if (length !== arr2.length) return false
  for (var i = 0; i < length; i++)
    if (arr1[i] !== arr2[i])
      return false
  return true
}

我可以确认src文件的路径是正确的,并且所有js文件都已加载。

1 个答案:

答案 0 :(得分:0)

要求茉莉花规格内的模块。

describe('$scope.lineIsValid', function() {
    var $scope,
        controller,
        yourModule = require('array-equal);

然后

expect(yourModule.equal(array0, array1)).toBe(true);