简体   繁体   English

使用独立的命令行界面与Jasmine一起测试angularjs

[英]Test angularjs with Jasmine using stand alone command line interface

I have a simple .js file and its test on Jasmine. 我有一个简单的.js文件及其对Jasmine的测试。 I'm able to run the test successfully through Jasmine's stand alone SpecRunner.html. 我可以通过Jasmine的独立SpecRunner.html成功运行测试。

Is it possible to do the same on command line interface? 可以在命令行界面上执行相同的操作吗? I do have node installed along with jasmine, 我确实已将节点与茉莉花一起安装,

npm install -g jasmine

Tests using Jasmine Command line interface works for simple javascript functions that return simple objects. 使用Jasmine命令行界面的测试适用于返回简单对象的简单javascript函数。 However if I start using angular libraries, it results in: 但是,如果我开始使用角度库,则会导致:

})(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 /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 /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);
    });
});

jasmine.json jasmine.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
}

Is it even possible to do what I'm trying to accomplish? 甚至有可能做我想完成的事情?

You need control it with Karma, if you wanna test angular in server side. 如果要在服务器端测试角度,则需要使用Karma进行控制。 Here an example angular and karma. 是一个角度和业力的例子。 Karma will simulate a browser. 业力将模拟浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM