1
  1. Какова наилучшая практика для ввода зависимостей в мой контроллер ?
  2. Когда я использую module(function($provide){})?
  3. Как правильно проверить, что $state.go() был вызван с правильными аргументами?

пример-controller.jsКонтроллер тестирования устройства с инжектированными зависимостями

angular.module('myModule') 
    .controller('ExampleCtrl', ['$state', 'ExampleService', 'exampleResolve', 
     function($state, ExampleService, exampleResolve){ 
     var self = this; 

     self.property = false; 
     self.resolvedProperty = exampleResolve; 

     self.submit = function() { 
      ExampleService 
      .exampleGet() 
      .$promise 
      .then(function(res) { 
       $state.go('anotherView', { prop1: 'yay', prop2: 'again' }); 
      }) 
     }; 
     }]); 

пример-controller.test.js

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; 
     exampleResolve = mockExampleResolve; 

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

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

     it('should exist', function() { 
      expect(!!ctrl).toBe(true); 
     }); 

     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? 
     }); 

     }); 

    }); 
+0

Что вы имеете в виду под названием с правильными аргументами? –

ответ

1

Вы можете использовать метод 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? 
     }); 

     }); 

    });