gpt4 book ai didi

javascript - 如何编写http请求的单元测试?

转载 作者:数据小太阳 更新时间:2023-10-29 05:29:35 26 4
gpt4 key购买 nike

我正在尝试对我的案例进行单元测试

在我的测试 Controller 中

myService.getItem('/api/toy/' + scope.id).success(
function(toy) {
$scope.toy = toys.details;
}
);

我的服务

angular.module('toyApp').service('myService', ['$http',
function($http) {
var service = {};
return {
getItem: function(url) {
return $http.get(url);
},
};
}
]);

测试文件。

describe('toy ctrl', function () {
var $httpBackend, ctrl, myService;

beforeEach(module('toyApp'));

beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_, __myService_) {
scope = _$rootScope_.$new();
$httpBackend = _$httpBackend_;
myService = _myService_;

ctrl = _$controller_('toyCtrl', {
$scope: scope
});

}));

describe('call my service', function() {
it('should make request when app loads', function() {
$httpBackend.expectGET('/api/toy/123').respond({id:123, detail:456 });
myService.getItem('/api/toy/123').then(function(toy){
expect(scope.toy.detail).toBe(456);
})
$httpBackend.flush();
})
})

我得到了

Error: Unexpected request: GET /api/toy/123
No more request expected

如果我取出$httpBackend.flush(),错误就消失了,但它不会覆盖

  function(toy) {
$scope.toy = toys.details;
}

部分。我想涵盖函数调用但不确定如何执行此操作。谁能帮我解决这个问题?非常感谢

最佳答案

似乎您正在对 Controller 进行“单元”测试,因此您不必引入图片中的服务,因为您只需要测试 Controller 逻辑。您可以创建模拟服务并在测试中创建 Controller 时注入(inject)它。

例子:

var mockItem = {details:{//somestuff}, id:'1'};// set up a mock object to test against later
//....
beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_, _$q_) {
scope = _$rootScope_.$new();
$httpBackend = _$httpBackend_;

//Set up mock
myService = jasmine.CreateSpyObj('myService', ['getItem']);
myService.getItem.and.returnValue($q.when(mockItem ));

ctrl = _$controller_('toyCtrl', {
$scope: scope,
myService: myService //<-- Pass it here
});

}));


//.....Assuming you are making the call when controller is instantiated
it('should make request when app loads', function() {
expect(myService.getItem).toHaveBeenCalled();
//You could also check as below
//expect(myService.getItem).toHaveBeenCalledWith(expectedidpassedin);
scope.$digest(); //Resolve $q promise callback
expect($scope.toy).toEqual(mockItem .details);
});

如果您专门单独对您的服务进行单元测试,您可以这样做:

it('should make request when app loads', function() {
var resp;
$httpBackend.expectGET('/api/toy/123').respond({id:123, detail:456});
myService.getItem('/api/toy/123').then(function(response){
resp = response.data;
});
$httpBackend.flush();
expect(resp.detail).toEqual(456);
});

在你的 Controller 中而不是链接 success 使用 then

 myService.getItem('/api/toy/' + scope.id).then(
function(response) {
$scope.toy = response.toys.details;
});

关于javascript - 如何编写http请求的单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30309804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com