gpt4 book ai didi

javascript - 测试基本的 Angular 服务

转载 作者:行者123 更新时间:2023-12-03 11:50:37 24 4
gpt4 key购买 nike

好的,这是我的设置:

var app = angular.module("KamajiDash", ['ngResource','ui.bootstrap'.. 'elasticsearch']);

app.service("elbService",function($http) {
this.remove_instance = function(elb_id, instance_id) {
$http.delete("/api/v1/amazon_elbs/"+elb_id+"/remove_instance/"+instance_id).success(function(data){
return data;
}).error(function(error){
return error;
});
}
});

这是我的 Jasmine 测试文件:

(function(){

var elbService, httpBackend;

describe ('elbService', function(){

beforeEach(function(){
module('KamajiDash');

inject(function(_elbService_,$httpBackend){
httpBackend = $httpBackend;
elbService = _elbService_;
});
});

describe("#remove_instances", function(){

beforeEach(function(){
httpBackend.when("DELETE","/api/v1/amazon_elbs/1/remove_instance/1").respond("success")
})

it("will provide a method called remove instances, that take two args, that calls $http.delete with the correct url", function(){
elbService.remove_instance(1,1).then(function(response){
expect(response).toEqual("success");
});
httpBackend.flush();
});

})

})

})();

因此,当我运行此 elbService.remove_instance(1,1) 时,返回未定义。有什么想法吗?

最佳答案

正如 @tymeJV 所提到的,您还需要返回 $http 对象或设置一个等于响应的范围变量。事实上,问题是你只返回一级,而不是两级:

a = function () {
function() { return 1; }() // 1 is visible here
}() // ... but not here

看到这个 fiddle :http://jsfiddle.net/v80dLmwz/

关于javascript - 测试基本的 Angular 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25856806/

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