如何使用$ location服务对angularjs控制器进行单元测试

时间:2012-12-01 21:59:11

标签: unit-testing angularjs

我正在尝试创建一个简单的单元测试来测试我的show函数。

我收到以下错误:

TypeError: Object #<Object> has no method 'show'

似乎$rootScope不是控制器的范围?

这是我的控制器:

function OpponentsCtrl($scope, $location) {
    $scope.show = function(url) {
        $location.path(url);
    }
}
OpponentsCtrl.$inject = ['$scope', '$location'];

这是我的控制器单元测试:

describe('OpponentsCtrl', function() {
    beforeEach(module(function($provide) {
        $provide.factory('OpponentsCtrl', function($location){
            // whatever it does...
        });
    }));

    it('should change location when setting it via show function', inject(function($location, $rootScope, OpponentsCtrl) {
        $location.path('/new/path');
        $rootScope.$apply();
        expect($location.path()).toBe('/new/path');

        $rootScope.show('/test');
        expect($location.path()).toBe('/test');
    }));
});

3 个答案:

答案 0 :(得分:63)

这就是我的测试结束工作的方式。

describe('OpponentsCtrl', function() {
    var scope, rootScope, ctrl, location;

    beforeEach(inject(function($location, $rootScope, $controller) {
        location = $location;
        rootScope = $rootScope;
        scope = $rootScope.$new();
        ctrl = $controller(OpponentsCtrl, {$scope: scope});
    }));

    it('should change location when setting it via show function', function() {
        location.path('/new/path');
        rootScope.$apply();
        expect(location.path()).toBe('/new/path');

        // test whatever the service should do...
        scope.show('/test');
        expect(location.path()).toBe('/test');

    });
});

答案 1 :(得分:21)

为什么不简单地使用spyOn函数?

describe('OpponentsCtrl', function() {

    var location;

    beforeEach(module(function($provide) {
        $provide.factory('OpponentsCtrl', function($location){
            location = $location;
        });
    }));

    it('should change location when setting it via show function', inject(function() {    
        spyOn(location, 'path');    
        expect(location.path).toHaveBeenCalledWith('/new/path');
    }));
});

希望这有帮助!

答案 2 :(得分:3)

我更喜欢模拟位置和服务,因为它是一个单元(非集成)测试:

'use strict';
describe('flightController', function () {
  var scope;
  var searchService;
  var location;

  beforeEach(module('app'));
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    mockSearchService();
    mockLocation();
    createController($controller);
  }));

  it('changes location to month page', function () {
    searchService.flightToUrl.and.returnValue('Spain/Ukraine/December/1');
    scope.showMonth();
    expect(location.url).toHaveBeenCalledWith('search/month/Spain/Ukraine/December/1');
  });

  function mockSearchService() {
    searchService = jasmine.createSpyObj('searchService', ['flightToUrl']);
  }

  function mockLocation() {
    location = jasmine.createSpyObj('location', ['url']);
  }

  function createController($controller) {
    $controller('flightController', {
      $scope: scope,
      searchService: searchService,
      $location: location
    });
  }
});

干杯

相关问题