具有注入依赖性的单元测试控制器

时间:2015-06-30 17:30:25

标签: angularjs unit-testing jasmine angular-ui-router karma-jasmine

    

  1. 将依赖项注入我的
控制器测试的最佳做法是什么?
  2. 

  3. 什么时候会我使用模块(函数($ provide){})
  4. 

  5. 如何正确检查 $ state使用正确的参数调用.go()
  6. 





example-controller.js




 <代码> angular.module( 'Mymodule中')&#XA; .controller('ExampleCtrl',['$ state','ExampleService','exampleResolve',&#xA; function($ state,ExampleService,exampleResolve){&#xA; var self = this;&#xA;&# xA; self.property = false;&#xA; self.resolvedProperty = exampleResolve;&#xA;&#xA; self.submit = function(){&#xA; ExampleService&#xA; .exampleGet()&#xA; 。$ promise&#xA; .then(function(res){&#xA; $ state.go('anotherView',{prop1:'yay',prop2:'again'});&#xA;})&# xA;};&#xA;}]);&#xA;  
&#xA;&#xA;

example-controller.test.js < / p>&#xA;&#xA;

  describe('Controller:ExampleCtrl',function(){&#xA; beforeEach(module('myModule'));&#xA;&#xA ; var ctrl,&#xA; mockBackend,&#xA; mockState;&#xA;&#xA; var mockExampleResolve = {test:'Test'};&#xA;&#xA; //提供所需的任何模拟&# xA; //我什么时候提供模拟?&#xA; beforeEach(function(){&#xA;模块(函数($ provide){&#xA;&#xA; });&#XA; });&#XA;&#XA; beforeEach(inject(函数($ controller,$ httpBackend,exampleResolve,$ state){&#xA; mockBackend = $ httpBackend;&#xA; mockState = $ state;&#xA; exampleResolve = mockExampleResolve;&#xA;&# xA; ctrl = $ controller('ExampleCtrl');&#xA;}));&#xA;&#xA; describe('initialization',function(){&#xA; beforeEach(function(){});&#xA;&#xA; it('should exists',function(){&#xA; expect(!! ctrl).toBe(true);&#xA;});&#xA;&#xA; it('应初始化任何视图模型变量',function(){&#xA; expect(ctrl.property)。 toBe('false');&#xA; expect(ctrl.resolvedProperty).toEqual({test:'Test'});&#xA;});&#xA;});&#xA;&#xA ; describe('submit called',function(){&#xA; beforeEach(function(){&#xA;&#xA;});&#xA;&#xA; it('应调用state.go with the正确的参数',function(){&#xA; //我如何检查这个?&#xA;});&#xA;&#xA;});&#xA;&#xA; });&#XA;  
&#XA;

1 个答案:

答案 0 :(得分:1)

您可以使用jasmine中的spyOn方法检查参数是否正确。

describe('Controller: ExampleCtrl', function() {
      beforeEach(module('myModule'));

      var ctrl,
          mockBackend,
          mockState;

      var mockExampleResolve = { test: 'Test' };

      // Provide any mocks needed
      // when do I provide mocks?
      beforeEach(function() {
        module(function($provide) {

        });
      });

      beforeEach(inject(function($controller, $httpBackend, exampleResolve, $state) {
        mockBackend = $httpBackend;
        mockState = $state;
        spyOn($state,'go');
        exampleResolve = mockExampleResolve;

        ctrl = $controller('ExampleCtrl');
      }));

      describe('initialization', function() {
        beforeEach(function() {});

        it('should exist', function() {
          expect(!!ctrl).toBe(true);
//Here you can pass your param and state
          expect($state.go).toHaveBeenCalledWith('anotherView', { prop1: 'yay', prop2: 'again' }); 
        });

        it('should initialize any view-model variables', function() {
          expect(ctrl.property).toBe('false');
          expect(ctrl.resolvedProperty).toEqual({test: 'Test'});
        });
      });

      describe('submit called', function() {
        beforeEach(function() {

        });

        it('should call state.go with the correct arguments', function() {
           // how do i check this?
        });

      });

    });