2

我在控制器中有一个功能:

function get(request) {
    return UserService.get({
                     id: request.id,
                     transformResponse: function(data) {
                         return angular.fromJson(data);
                     }
                    });
};

并像这样测试:

var $scope;
var controller;
var UserService;

beforeEach(function() {
   angular.mock.module(function($provide) {
       UserService = jasmine.createSpyObj('UserService', ['get']);

       $provide.value('UserService', UserService);
   });
});

beforeEach(inject(function($rootScope, $controller, UserService) {
    $scope = $rootScope.$new();
    controller = $controller('...' {
        $scope: $scope,
        UserService: UserService
    });

    $scope.$digest();
}));

it('should call user service get function when getting user', function() {
    var request = { id: 5 };
    controller.get(request);

    expect(UserService.get).toHaveBeenCalledWith(request);
});

测试通过,但业力覆盖显示该函数 transformResponse 未被测试覆盖。我必须如何正确模拟这个函数?谢谢!

4

0 回答 0