34

我正在尝试创建一个简单的单元测试来测试我的 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');
    }));
});
4

3 回答 3

65

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

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');

    });
});
于 2012-12-01T21:59:11.060 回答
20

你为什么不简单地使用一个 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');
    }));
});

希望这可以帮助!

于 2013-10-25T17:30:22.653 回答
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
    });
  }
});

干杯

于 2015-01-12T21:01:33.010 回答