2017-10-21 8 views
0

私は単純な.jsファイルとJasmineでのテストを行っています。私はJasmineのスタンドアロンのSpecRunner.htmlを通してテストを成功させることができます。スタンドアローンのコマンドラインインターフェイスを使用してJasmineでangleJsをテスト

コマンドラインインターフェイスでも同じことを実行できますか?私はジャスミンコマンドラインインタフェースを使用して

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角度とカルマの例。 カルマはブラウザをシミュレートします。

関連する問題