0

下面是使用 $resource 从 web api 获取值的服务

(function () {
'use strict';
function a($resource) {
    return $resource('api/values/a');
}

angular
    .module('app')
    .factory('a', ['$resource', a]);}());

我有一个类似上述服务的服务列表

以下是使用上述服务获取值的服务,它将作为单个结果返回我如何使用 jasmine 单元测试测试以下服务我需要以下服务的代码覆盖率

(function () {
'use strict';

function factoryService($q, a, b, c, d) {
    function fetch(id) {

        var data = {},
            promises = [];



        data.avalues = a.query();
        promises.push(data.avalues.$promise);

         data.bvalues = b.query();
        promises.push(data.bvalues.$promise);

        data.cvalues = c.query();
        promises.push(data.cvalues.$promise);

        data.dvalues = d.query();
        promises.push(data.dvalues.$promise);

        return $q.all(promises).then(function () {

            return data;
        });
    }

    return {
        fetch: fetch
    };
}

angular
    .module('app')
    .factory('factoryService', ['$q', 'a', 'b', 'c', 'd', factoryService]);}());

任何人都可以帮我为上述工厂服务编写具有代码覆盖率的单元测试

describe('从服务中获取 a,b,c,d 值', function() {

  var service,result,httpBackend,rootScope,$q,scope;

  beforeEach(module('app'));

  beforeEach(inject(function(proposalPlanInitialData, _$httpBackend_,$rootScope, _$q_) {
    $q = _$q_;
    service = proposalPlanInitialData;
    httpBackend = _$httpBackend_;
    rootScope = $rootScope;
    scope = $rootScope.$new();
    deferred = _$q_.defer();
  }));


  it ('should be loaded', function() {
      expect(service).toBeDefined();
  });

  it ('should return get data when calling fetch', function() {
    fakeListing = {
    id: 123,
    price: 300000
    };
     var getData = { "id": '1', "name": "dummyvalue" };
    httpBackend.expectGET('api/values/a').respond({ "id": '1', "name": "dummyvalue" });
    httpBackend.expectGET('api/values/b').respond({ "id": '1', "name": "dummyvalue" });
    httpBackend.expectGET('api/values/c').respond({ "id": '1', "name": "dummyvalue" });
    httpBackend.expectGET('api/values/d').respond({ "id": '1', "name": "dummyvalue" });



    var data = service.fetch(1,1);

    spyOn(service, 'fetch').and.callFake(function () {
    return fakeListing;
    });        
    // httpBackend.flush();
    var data = service.fetch(1);
    expect(data).toBe(fakeListing);});});

但得到一个未定义的数据

4

0 回答 0