gpt4 book ai didi

javascript - 如何在 jasmine 中测试 $http 服务

转载 作者:行者123 更新时间:2023-11-28 21:27:02 25 4
gpt4 key购买 nike

在我的代码下面我创建了小型 MEAN 应用程序。我不知道如何测试服务。请帮助我

申请代码

var app = angular.module('guestPhoneBook',[]);

app.config(function($interpolateProvider,$compileProvider){
$interpolateProvider.startSymbol("{[{");
$interpolateProvider.endSymbol("}]}");
$compileProvider.debugInfoEnabled(false);
});

app.run(function($rootScope){
$rootScope.company = "IGATE";
});

app.directive('guestDir',function(){
return function($scope, GuestDatas, $http){

var refresh = function(){
var promise = GuestDatas.getAllGuest();
promise.success(function(data){
$scope.guestList = data;
$scope.newContact ="";
});
}
refresh();

$scope.addNewContact = function(){
promise = GuestDatas.addNewGuest($scope.newContact);
promise.success(function(){
refresh();
});

}

$scope.deleteGuest = function(id){
promise = GuestDatas.deleteGuest(id);
promise.success(function(data){
refresh();
});
}
$scope.edit = function(id){
promise = GuestDatas.editGuest(id);
promise.success(function(data){
$scope.updateShow = true;
$scope.newContact = data;
});
};

$scope.updateGuestData = function(){
//console.log($scope.newContact);
promise = GuestDatas.updateGuest($scope.newContact);
promise.success(function(data){
refresh();
});
}
}
});

/*
app.service('AddService',function(){
this.add = function(a,b){
return a+b;
}
});
*/
app.controller('guestCtrl',function($scope){
$scope.updateShow = false;
//$scope.addResult = AddService.add(5,6);
});

app.factory("GuestDatas",function($http){
return {
getAllGuest : function(){
var url = '/guestDetails';
return $http.get(url);
},
addNewGuest:function(newGuest){
var url = '/guestDetails';
return $http.post(url,newGuest);
},
deleteGuest:function(id){
var url ='/guestDetails/'+id;
return $http.delete(url);
},
editGuest:function(id){
var url='/guestDetails/'+id;
$http.put(url);
},
updateGuest:function(guestData,fn){
console.log(guestData);
var url = "/guestDetails/"+guestData._id;
$http.put(url,guestData);
}
}
});

测试文件:

describe("Testing app.js", function() {
var myController;
var scope;
var rootScope,httpBackend,factory;

beforeEach(module('guestPhoneBook'));

beforeEach(inject(function($controller,$rootScope,GuestDatas,$http,$httpBackend){
rootScope = $rootScope;
scope = rootScope.$new();
myController = $controller('guestCtrl',{$scope:scope});
factory = GuestDatas;
httpBackend = $httpBackend;
}));

it('should have controller named guestCtrl',function(){
expect(myController).toBeDefined();
});

it('should have updateShow object and attached to scope',function(){
expect(scope.updateShow).toBeDefined();
expect(scope.updateShow).toBeFalsy();
});
it("API method to be defined",function(){
expect(factory.getAllGuest).toBeDefined();
expect(factory.addNewGuest).toBeDefined();
expect(factory.deleteGuest).toBeDefined();
expect(factory.editGuest).toBeDefined();
expect(factory.updateGuest).toBeDefined();
});
it("API method to be function",function(){
expect(factory.getAllGuest).toEqual(jasmine.any(Function));
expect(factory.addNewGuest).toEqual(jasmine.any(Function));
expect(factory.deleteGuest).toEqual(jasmine.any(Function));
expect(factory.editGuest).toEqual(jasmine.any(Function));
expect(factory.updateGuest).toEqual(jasmine.any(Function));
//factory.getAllGuest(fn);
});
it("testing http call", function(){
httpBackend.when('GET', '/guestDetails').respond(200,{});
expect(httpBackend.flush).not.toThrow();
//httpBackend.expect('GET', '/guestDetails').respond(1200);
});
});

请帮助我测试指令中的服务。在我的测试文件中。我保留了本地 j_son 数据并尝试进行测试,但仍然没有得到正确的响应。

在此先感谢亲爱的 friend ..AK..

最佳答案

您可以使用模拟服务$httpBackend 来测试Jasmine 中的$http

关于javascript - 如何在 jasmine 中测试 $http 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37685133/

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