2015-04-14 39 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); 
     }); 
    }); 
)};