【问题标题】:Test angularjs with Jasmine using stand alone command line interface使用独立的命令行界面使用 Jasmine 测试 angularjs
【发布时间】:2018-04-02 17:56:39
【问题描述】:

我有一个简单的 .js 文件及其在 Jasmine 上的测试。我可以通过 Jasmine 的独立 SpecRunner.html 成功运行测试。

是否可以在命令行界面上做同样的事情?我确实与茉莉花一起安装了节点,

npm install -g jasmine

使用 Jasmine 命令行界面进行测试适用于返回简单对象的简单 javascript 函数。但是,如果我开始使用角度库,则会导致:

})(window, document);
   ^

ReferenceError: window is not defined
    at Object.<anonymous> (/spec/helpers/lib/angularjs/1_4_4/angular.js:28602:4)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at /usr/local/nodejs/node-v6.11.4-darwin-x64/lib/node_modules/jasmine/lib/jasmine.js:99:5
    at Array.forEach (native)

/helpers/jasmine-examples/my-sourcefile.js

var myModule = angular.module('myApplication', []);

myModule.controller('SimpleController', function($scope) {
        $scope.amount = ['a','b','c'];
});

/helpers/jasmine-examples/my-sourcefile.spec.js

describe('a simple controller', function(){
    var $scope;

    //See API angular.mock.module
    beforeEach(module('myApplication'));

    beforeEach( inject( function( $rootScope, $controller ){
        $scope = $rootScope.$new();
        $controller('SimpleController',{$scope : $scope});
    }));

    it('test scope amount', function(){
        expect($scope.amount.length).toBe(3);
    });
});

茉莉花.json

{
  "spec_dir": "spec",
  "spec_files": [
    "**/*[sS]pec.js"
  ],
  "helpers": [
    "helpers/jasmine_examples/*.js",
    "helpers/lib/angularjs/1_4_4/angular.js",
    "helpers/lib/angularjs/1_4_4/angular-mocks.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": false
}

是否有可能做我想要完成的事情?

【问题讨论】:

  • 问题在于 jasmine 没有运行测试的 DOM。我建议使用带有 jsdom 实现的 Jest

标签: angularjs node.js unit-testing jasmine


【解决方案1】:

如果你想在服务器端测试角度,你需要用 Karma 来控制它。 Here 角度和业力的例子。 Karma 将模拟浏览器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-23
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    相关资源
    最近更新 更多