Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 测试基本角度服务_Javascript_Angularjs_Jasmine_Httpbackend - Fatal编程技术网

Javascript 测试基本角度服务

Javascript 测试基本角度服务,javascript,angularjs,jasmine,httpbackend,Javascript,Angularjs,Jasmine,Httpbackend,好的,这是我的设置: 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

好的,这是我的设置:

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();
      });  

    })  

  })  

})();

因此,当我运行此服务时,remove_实例(1,1)返回未定义。知道为什么吗?

正如@tymeJV在那里提到的,您还需要返回$http对象,或者设置一个与响应相等的范围变量。问题是,您只返回一个级别,而不是两个级别:

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

查看此小提琴:

您不需要
同时返回
$http
请求对象:
返回$http.delete吗(“/api/v1/amazon_
啊,我在那里输入了一个错误。我更新了它。你介意再看一次服务吗……我没有具体返回函数,但我不确定为什么需要返回。所以我基本上在http$.delete()请求之前添加了return这个词。所有的问题都得到了解决。谢谢大家:)