Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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/25.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 Jasmine测试在AngularJS工厂失败_Javascript_Angularjs_Jasmine - Fatal编程技术网

Javascript Jasmine测试在AngularJS工厂失败

Javascript Jasmine测试在AngularJS工厂失败,javascript,angularjs,jasmine,Javascript,Angularjs,Jasmine,我在AngularJS中创建了以下基本工厂,用于检索对象数组: angular. module('core.beer-retriever'). factory('Beer', ['$resource', ($resource) => { return $resource('https://www.xxxxxxxxx.com/v2/beers', {}, { query: { method: 'GET', isArray: true

我在AngularJS中创建了以下基本工厂,用于检索对象数组:

angular.
module('core.beer-retriever').
factory('Beer', ['$resource',
  ($resource) => {
    return $resource('https://www.xxxxxxxxx.com/v2/beers', {}, {
      query: {
        method: 'GET',
        isArray: true
      },
    });
  },
]);
我用Jasmine创建了以下测试:

  describe('Beer Service', function() {
    beforeEach(angular.mock.module('core.beer-retriever'));

    beforeEach(inject(function($httpBackend) {
      let beersRetrieved = {
        results: [{
          "id": 192,
          "name": "Punk IPA 2007 - 2010",
        }]
      };
      $httpBackend.whenGET('https://api.xxxxxxxxx.com/v2/beers')
        .respond(beersRetrieved);
    }));

    it('is defined', inject(function(Beer) {
      expect(Beer).toBeDefined();
    }));

    it('returns a resource function', inject(function(Beer) {
      const output = Beer;
      expect(output).toEqual(jasmine.any(Function));
      expect(output.query({}.$promise)).toBeDefined();
    }));

    it('returns beers',
      inject(function(Beer) {
        const output = Beer.query();
        expect(output.length).toEqual(1);
        expect(output[0].id).toEqual(192);
        httpBackend.flush();
      }));
  });
前两项测试成功,但第三项测试失败:

Expected 0 to equal 1.
TypeError: Cannot read property 'id' of undefined
            at Object.<anonymous> (app/core/beer-retriever/beer-retriever.spec.js:29:25)
            at Object.invoke (app/bower_components/angular/angular.js:4862:19)
            at Object.WorkFn (app/bower_components/angular-mocks/angular-mocks.js:3170:20)
预期0等于1。
TypeError:无法读取未定义的属性“id”
反对。(app/core/beer retriever/beer retriever.spec.js:29:25)
在Object.invoke(app/bower_components/angular/angular.js:4862:19)
在Object.WorkFn(app/bower_components/angular mocks/angular mocks.js:3170:20)

如何找出问题所在?

需要向资源承诺对象添加解析函数。 结帐这个工作

完整规格如下所示:

  it('returns beers',
    inject(function(Beer, $httpBackend) {
      var output = Beer.query();
      output.$promise.then(function(data) {
        console.log(data);
        output = data[0].results;
      });
      $httpBackend.flush();
      console.log('a11: ' + JSON.stringify(output));
      expect(output.length).toEqual(1);
      console.log(output[0].id);
      expect(output[0].id).toEqual(192);
    }));

您尚未刷新$httpBackend。什么意思?您需要$httpBackend.flush()才能使模拟响应生效。我已添加:$httpBackend.flush();$httpBackend.whenGET(').respond(beersretrieve)之后;但是现在所有3个测试都失败了,没有必要这样做(我很确定它在错误中说了为什么你不应该这样做)。您应该在执行请求后添加它,即Beer.query()。
  it('returns beers',
    inject(function(Beer, $httpBackend) {
      var output = Beer.query();
      output.$promise.then(function(data) {
        console.log(data);
        output = data[0].results;
      });
      $httpBackend.flush();
      console.log('a11: ' + JSON.stringify(output));
      expect(output.length).toEqual(1);
      console.log(output[0].id);
      expect(output[0].id).toEqual(192);
    }));