2017-10-21 78 views
0

我有一个简单的.js文件及其在Jasmine上的测试。我可以通过Jasmine独立的SpecRunner.html成功运行测试。使用独立命令行界面与Jasmine一起测试angularjs

是否可以在命令行界面上执行相同的操作?我有茉莉花一起安装节点,使用茉莉花命令行界面

npm install -g 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); 
    }); 
}); 

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 
} 

它甚至有可能做我想要完成的事情吗?

+1

问题是茉莉花没有运行测试的DOM。我建议使用带有jsdom实现的[Jest](https://facebook.github.io/jest/)。 –

回答

0

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

相关问题