2014-07-12 1 views
3

저는 AngularJS로 몇 가지 테스트를 시작했으나 아직 완전히 이해하고 있다고 말할 수는 없습니다. 내 공장에서 REST API 메소드를 테스트하고 싶지만 오류가 발생하고 있습니다. 내가 뭘 잘못하고 있는지 모르겠다. 누구든지 나를 도울 수 있습니까?

내 공장 :

'use strict'; 

angular.module('corsaneApp') 
    .factory('resourceService', ['$http', '$log', '$resource', '$sce', 'listService', 'global', 'config', 
    function($http, $log, $resource, $sce, listService, global, config) { 

     var search = null; 

     return { 
     post: function(name, url, type, res) { 
      var prefix = 'http'; 
      if (url.substr(0, prefix.length) !== prefix) { 
      url = prefix + '://' + url; 
      $log.info('NewUrl ' + url); 
      } 
      // Post resource 
      $http.post(config.apiBaseUrl + 'resources?name=' + name + '&url=' + url + '&format=' + type).success(function(data) { 
      var bool = global.toPlaylist(); 
      $log.info("Data" + data.url + data.name + 'Bool' + bool.value); 
      alert('Success'); 
      if (bool.value) { 
       var playlist = global.setList(); 
       listService.addListElement(data, playlist.value); 
       res.push(data); 
      }; 
      }).error(function(error, data, status, config) { 
      $log.info("It doesnt work!" + data + config); 
      }); 

     }, 
     get: function() { 
      return search; 
     }, 
     search: function(term) { 
      var tmp = $resource(config.apiBaseUrl + 'resources/' + term, { 
      id: '@id' 
      }, {}); 
      search = tmp.query(); 
      return tmp.query(); 
     }, 
     show: function(item) { 

      // Assign a resource to selected variable. 
      global.setResource(item); 


     } 
     } 
    } 
    ]); 

테스트 파일 :

'use strict'; 

describe('Service: resource', function() { 

    // load the service's module 
    beforeEach(module('corsaneApp')); 

    // instantiate service 
    var $httpBackend; 
    var conf; 
    var getResource; 
    var tmp; 
    var resource; 

    beforeEach(inject(function($rootScope, _$httpBackend_, $http, config, resourceService) { 
    conf = config; 
    $httpBackend = _$httpBackend_; 
    resource = resourceService; 

    })); 

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

    it('should return a list of resources', function() { 
    $httpBackend.expectGET(conf.apiBaseUrl + 'resources/queen').respond({ 
     "id": 1, 
     "url": "https://en.wikipedia.org/wiki/Elizabeth", 
     "text": "no written-explanationobject", 
     "name": "queen 1" 
    }, { 
     "id": 2, 
     "url": "https://en.wikipedia.org/wiki/Elizabeth_II", 
     "text": "no written-explanationobject", 
     "name": "queen 2" 
    }); 
    tmp = resource.search('queen'); 
    tmp.$promise.then(function(data) { 
     getResource = data; 
    }) 
    $httpBackend.flush(); 

    expect(getResource[0].id).toBe(1); 
    }); 

}); 

오류 메시지 :

Chrome 35.0.1916 (Mac OS X 10.9.3) Service: resource should return a list of resources FAILED 
    Error: Unexpected request: GET http://localhost:8888/Corsane/web/app_dev.php/api/resources/queen 
    No more request expected 
     at $httpBackend (/Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular-mocks/angular-mocks.js:1177:9) 
     at sendReq (/Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular/angular.js:8263:9) 
     at $http.serverRequest (/Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular/angular.js:7995:16) 
     at wrappedCallback (/Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular/angular.js:11485:81) 
     at wrappedCallback (/Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular/angular.js:11485:81) 
     at /Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular/angular.js:11571:26 
     at Scope.$eval (/Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular/angular.js:12595:28) 
     at Scope.$digest (/Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular/angular.js:12407:31) 
     at Function.$httpBackend.flush (/Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular-mocks/angular-mocks.js:1435:16) 
     at null.<anonymous> (/Users/oyvindhellenes/Documents/Corsane/test/spec/services/resource.js:43:18) 
    Error: Unflushed requests: 1 
     at Function.$httpBackend.verifyNoOutstandingRequest (/Users/oyvindhellenes/Documents/Corsane/app/bower_components/angular-mocks/angular-mocks.js:1489:13) 
     at null.<anonymous> (/Users/oyvindhellenes/Documents/Corsane/test/spec/services/resource.js:24:18) 
Chrome 35.0.1916 (Mac OS X 10.9.3): Executed 1 of 1 Chrome 35.0.1916 (Mac OS X 10.9.3): Executed 1 of 1 (1 FAILED) ERROR (0.053 secs/0.051 secs) 
Warning: Task "karma:unit" failed. Use --force to continue. 

Aborted due to warnings. 

답변

3

그것은 문제가 솔기는 검색 기능에 두 번 요청을 호출 할 것입니다.

$httpBackend.verifyNoOutstandingRequest을 사용합니다. 테스트에서 예상보다 많은 요청이 없는지 확인합니다.

다음에 변화를 시도 할 수 있었다를 해결하려면 :

search: function(term) { 
      var tmp = $resource(config.apiBaseUrl + 'resources/' + term, { 
      id: '@id' 
      }, {}); 
      search = tmp.query(); 
      return search; 
     }, 

죄송합니다, 제가 지금 직접 확인하지 가능성이 있지만, 그것은 일

+0

이 문제를 해결해야한다. 감사 ;) –

관련 문제