簡體   English   中英

使用獨立的命令行界面與Jasmine一起測試angularjs

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

我有一個簡單的.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);
    });
});

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
}

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

如果要在服務器端測試角度,則需要使用Karma進行控制。 是一個角度和業力的例子。 業力將模擬瀏覽器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM