Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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 TypeError:angularjs服务单元测试中未定义解析_Javascript_Angularjs_Unit Testing_Jasmine_Angularjs Service - Fatal编程技术网

Javascript TypeError:angularjs服务单元测试中未定义解析

Javascript TypeError:angularjs服务单元测试中未定义解析,javascript,angularjs,unit-testing,jasmine,angularjs-service,Javascript,Angularjs,Unit Testing,Jasmine,Angularjs Service,我正在尝试对使用$http的服务进行单元测试。我正在使用Jasmine,并且不断出现以下错误: TypeError:angular.js(第13737行)中未定义解析 这就是我的服务的样子: angular.module('myapp.services', []) .factory('inviteService', ['$rootScope', '$http', function($rootScope, $http) { var inviteService = { tok

我正在尝试对使用$http的服务进行单元测试。我正在使用Jasmine,并且不断出现以下错误:

TypeError:angular.js(第13737行)中未定义解析

这就是我的服务的样子:

angular.module('myapp.services', [])
.factory('inviteService', ['$rootScope', '$http', function($rootScope, $http) {
    var inviteService = {
        token: '',

        getInvite: function(callback, errorCallback) {
            $http.get('/invites/' + this.token + '/get-invite')

            .success(function(data) {
                callback(data);
            })

            .error(function(data, status, headers, config) {
                errorCallback(status);
            });
        }
    };

    return inviteService;
}]);
describe ('Invite Service', function () {
  var $httpBackend, inviteService, authRequestHandler;

  var token = '1123581321';

  beforeEach(module('myapp.services'));

  beforeEach(inject(function ($injector) {
    $httpBackend = $injector.get('$httpBackend');
    authRequestHandler = $httpBackend.when('/invites/' + token + '/get-invite').respond({userId: 'userX'}, {'A-Token': 'xxx'});
    inviteService = $injector.get('inviteService');
  }));

  afterEach (function () {
    $httpBackend.verifyNoOutstandingExpectation ();
    $httpBackend.verifyNoOutstandingRequest ();
  });

  describe ('getInvite', function () {
    beforeEach(function () {
      inviteService.token = token;
    });

    it ('should return the invite', function () {
      $httpBackend.expectGET('/invites/' + token + '/get-invite');
      inviteService.getInvite();
      $httpBackend.flush();
    });
  });
});
这就是我的测试结果:

angular.module('myapp.services', [])
.factory('inviteService', ['$rootScope', '$http', function($rootScope, $http) {
    var inviteService = {
        token: '',

        getInvite: function(callback, errorCallback) {
            $http.get('/invites/' + this.token + '/get-invite')

            .success(function(data) {
                callback(data);
            })

            .error(function(data, status, headers, config) {
                errorCallback(status);
            });
        }
    };

    return inviteService;
}]);
describe ('Invite Service', function () {
  var $httpBackend, inviteService, authRequestHandler;

  var token = '1123581321';

  beforeEach(module('myapp.services'));

  beforeEach(inject(function ($injector) {
    $httpBackend = $injector.get('$httpBackend');
    authRequestHandler = $httpBackend.when('/invites/' + token + '/get-invite').respond({userId: 'userX'}, {'A-Token': 'xxx'});
    inviteService = $injector.get('inviteService');
  }));

  afterEach (function () {
    $httpBackend.verifyNoOutstandingExpectation ();
    $httpBackend.verifyNoOutstandingRequest ();
  });

  describe ('getInvite', function () {
    beforeEach(function () {
      inviteService.token = token;
    });

    it ('should return the invite', function () {
      $httpBackend.expectGET('/invites/' + token + '/get-invite');
      inviteService.getInvite();
      $httpBackend.flush();
    });
  });
});
我对基于angularjs的应用程序的单元测试非常陌生,我在angularjs文档中使用了这个例子

$httpBackend


我不确定我可能会错过什么,我已经尝试了不同的事情,我总是得到相同的错误,任何帮助将不胜感激

解析的
变量是来自相关服务的URL。由于以下原因之一,它未定义:

  • URL格式不正确
  • 未调用$http.get
  • 未定义令牌
  • 成功
    错误
    回调没有
    数据
  • 未调用
    .respond
  • .respond
    不包含响应对象作为参数
  • 例如:

     describe('simple test', test);
    
     function test()
       {
       it('should call inviteService and pass mock data', foo);
    
       function foo() 
         {
         module('myapp.services');
         inject(myServiceTest);
    
         function myServiceTest(inviteService, $httpBackend)
           {
           $httpBackend.expect('GET', /.*/).respond(200, 'bar');
    
           function callback(){};
    
           inviteService.getInvite.token = '1123581321';
           inviteService.getInvite(callback, callback);
    
           $httpBackend.flush();
    
           expect(callback).toHaveBeenCalledOnce();
           }
         }
       }
    
    参考资料


    我制作了一把小提琴,效果非常好。也许问题不在考试本身。我可以问一下你的angularjs版本是什么吗?嘿,很抱歉耽搁了,我仍然有问题,但我可以看到你在小提琴上工作,我正在使用angularjs的v1.2.11。谢谢嗯,我已经用你的angulr版本更新了小提琴,但它仍然可以。链接:。在这一行“$httpBackend.expectGET('/invests/'+token+'/get invite');”中需要注意的一点是,您忘了调用respond,就像我在小提琴上做的那样。除了一切正常之外,您还可以查看代码中调用解析变量的地方,或者它可能与Angularjs中的$parse服务有关