1

我试图通过传递一个虚拟版本的服务来测试我的控制器。但是,当 karma 运行时,它会调用在控制器定义(第 3 行)中指定的真实服务,而不是我尝试注入的对象beforeEach(inject(function(....)

请帮助我确定我做错了什么。

//I have a code like the following.
angular.module('myApp')
    .controller('memberSearch2Ctrl', ['$scope', 'PersonnelService', 'SavedSearchService', '$routeParams', '$log',
        function memberSearch2Ctrl($scope, personnelApi, savedSearches, $routeParams, $log) {
            // utilises PersonnelService to make ajax calls to get data from server....
        }
    ]);

// real version of a service that makes ajax calls and returns real data
angular.module('myApp')
    .service('PersonnelService', function($http, $q) {
        //this.search(....)
    });

// dummy version of the above, has the same functions like above just returns hardcoded json
angular.module('myApp')
    .service('PersonnelServiceMock', function($http, $q) {
        // returns hardcoded json for testing purpose
        //this.search(....)
    });


// heres my tests
describe('memberSearch2Ctrl', function() {
    var ctrl, scope, personnelApiMock, savedSearchService;

    beforeEach(module('myApp'));
    beforeEach(inject(function($rootScope, $controller, PersonnelServiceMock, SavedSearchService, $log) {

        personnelApiMock = PersonnelServiceMock; // this sets the PersonnelServiceMock correctly
        console.log(JSON.stringify(PersonnelServiceMock)); // as I see in this line
        console.log(JSON.stringify(SavedSearchService));


        scope = $rootScope.$new();
        ctrl = $controller('memberSearch2Ctrl', {
            $scope: scope,
            personnelApi: PersonnelServiceMock,
            savedSearches: SavedSearchService,
            $routeParams: {},
            $log: $log
        });

    }));

    iit('upon search $scope.searchResults = PersonnelService.searchPaged(...)', function() {
        // however problem lies in the next line
        scope.search(); //  this calls PersonnelService.search insted of PersonnelServiceMock.search
        // even when I have beforeEach(inject(function($rootScope, $controller, >>> PersonnelServiceMock <<<,
        scope.$root.$digest();

        var expected = personnelApiMock.searchPaged(null, null, null);
        var actual = scope.searchResults;
        expect(actual).toEqual(expected);
    });
});

尝试传入 $injector 并让注入器实例化 PersonnelServiceMock。控制台日志说我实际上得到了 PersonnelServiceMock 作为回报。但它仍然尝试进行 PersonnelService 中定义的 ajax 调用

 beforeEach(inject(function($rootScope, $controller, $injector) {

    personnelApiMock = $injector.get('PersonnelServiceMock');
    savedSearchService = $injector.get('SavedSearchService');
    log = $injector.get('$log');

    console.log(JSON.stringify(personnelApiMock));
    console.log('==========================================');
    console.log(JSON.stringify(savedSearchService));
    console.log('******************************************');

    scope = $rootScope.$new();
    ctrl = $controller('memberSearch2Ctrl', {
      $scope: scope,
      personnelApi: personnelApiMock,
      savedSearches: savedSearchService,
      $routeParams: {},
      $log: log
    });

}));

似乎调用中指定的ctrl = $controller('memberSearch2Ctrl', { ...})内容被忽略,并且正在使用控制器定义(第~3行)中指定的内容。

4

1 回答 1

1

问题在于这段代码:

ctrl = $controller('memberSearch2Ctrl', {
    $scope: scope,
    personnelApi: PersonnelServiceMock, <!---
    savedSearches: SavedSearchService,
    $routeParams: {},
    $log: $log
});

应该:

ctrl = $controller('memberSearch2Ctrl', {
    $scope: scope,
    PersonnelService: PersonnelServiceMock, <!---
    savedSearches: SavedSearchService,
    $routeParams: {},
    $log: $log
});
于 2013-07-22T07:23:29.387 回答