2015-04-14 141 views
0

我想在我的Angular應用程序上設置Jasmine測試來測試控制器。茉莉花測試不運行

控制器:

var navigation = angular.module("navigation", []); 

navigation.controller("NavigationController", ['$scope', function ($scope) { 
    $scope.myObject = []; 
    $scope.tabs = [ 
     { title: "Surcharge Index", content: "SurchargeIndex" }, 
     { title: "Scheduling and Adjustments", content: "Scheduling" }, 
     { title: "Auto Update Settings", content: "Automation" }, 
     { title: "Processing Rules", content: "FuelProcessing" }, 
     { title: "Data Update ", content: "DataUpdate" }, 

    ]; 
}]); 

測試:

describe("NavigationController", function() { 
    var scope; 
    var controller; 

    //beforeEach(module('app')); 

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

    it("scope is defined", function() { 
     expect(scope).toBeDefined(); 
     //expect(scope.tags[0].title).toBe('Doe Index'); 
    }); 

    it("should contain a list of tabs", function() { 
     //expect(scope).toBeDefined(); 
     expect(scope.tags).toContain({ title: 'Doe Index' }); 
    }); 

}); 

無論茉莉花測試有史以來運行。

測試頁:

Jasmine2.0.0finished in 0.001s 
raise exceptions 
Ran 0 of 2 specs - run all 
0 specs, 0 failures 
NavigationController 
scope is defined 
should contain a list of tabs 

這是茉莉花返回。出於某種原因,沒有任何測試正在運行。

有什麼建議嗎?

+0

看起來好像是在jasmine執行測試後運行代碼的測試塊。不知道爲什麼...... –

回答

1

你必須加載模塊要測試:

beforeEach(module('navigation')); 

補充一點,你必須:

//beforeEach(module('app')); 

但註釋。

+0

感謝您的幫助。雖然這沒有完美的工作,但它確實讓我朝着正確的方向前進。根據您的建議,我使用以下博客文章作爲模型。 [使用AngularJS進行簡單單元測試](http://odetocode.com/blogs/scott/archive/2013/06/10/simple-unit-tests-with-angularjs.aspx) – James

+0

沒問題,很高興我能幫上忙。我建議你用正確的解決方案來回答你自己的問題,那樣,當別人偶然發現同樣的問題併到達這裏時,他們會知道如何解決它;) – taxicala

0

我用這個OdeToCode後作爲樣本,一旦我意識到我需要加載模塊。我的測試用例現在看起來像這樣。

describe("myApp", function() { 
    beforeEach(module('navigation')); 

    describe("NavigationController", function() { 
     var scope; 
     var controller; 

     beforeEach(inject(function($controller, $rootScope) { 
      jasmine.addMatchers(customMatcher); 
      scope = $rootScope.$new(); 
      controller = $controller('NavigationController', { '$scope': scope }); 
     })); 

     it("scope is defined", function() { 
      expect(scope).toBeDefined(); 
     }); 

     it("should contain a list of tabs", function() { 
      expect(scope.tabs.length).toBe(5); 
     }); 
    }); 
)};