2016-08-30 3 views
1

$ httpBackend를 사용하여 조롱 할 때 반환되는 데이터 유형을 정확히 모르는 경우 모의 작업을 어떻게 만들 수 있습니까? http 호출이 컨트롤러 초기화에있게하고 싶습니다. 한 가지주의 할 점은 json 객체가 반환된다는 것입니다.

(function() { 
    'use strict'; 

    angular 
     .module('app') 
     .service('dataService', dataService); 


    function dataService($http) { 
     this.getMovies = getMovies; 

     //////////////// 

     function getMovies() { 
      return $http.get('./src/app/movies/data.json') 
       .then(function (response) { 
        return response.data 
       }) 
     } 
    }; 
})(); 

;

(function() { 
    'use strict'; 

    angular.module('app') 
     .controller('moviesController', moviesController); 

    moviesController.$inject = ['dataService']; 

function moviesController(dataService) { 
    var vm = this 

    vm.movies; 

    vm.getMovies = getMovies; 
    getMovies(); 

    function getMovies() { 
     return dataService.getMovies() 
      .then(function (data) { 
       return vm.movies = data.movies; 
      }); 
    } 
}; 
}()); 

;

describe('moviesController', function() { 

    var $controller, 
     moviesController, 
     dataService, 
     $httpBackend; 

    beforeEach(angular.mock.module('app')); 
    beforeEach(angular.mock.module('ui.router')); 

    beforeEach(inject(function (_$controller_, _dataService_, _$httpBackend_) { 
     $controller = _$controller_; 
     dataService = _dataService_; 
     $httpBackend = _$httpBackend_; 

     moviesController = $controller('moviesController', { dataService: dataService }); 
    })) 

    it('should be defined', function() { 
     expect(moviesController).toBeDefined(); 
    }); 

it('should initialise with a call to dataService.getMovies()', function() { 
    var url = "./src/app/movies/data.json"; 
    var movies = {}; 
    $httpBackend.expectGET(url).respond(200, movies); 
    moviesController.getMovies(); 
    expect(moviesController.movies).toEqual(movies); 
    $httpBackend.flush(); 

}); 

}); 

;

Expected undefined to equal Object({ }). 

답변

0

사양에 정의한 개체로 반환을 설정할 수 있습니다.

var response = {}; 
it('should initialise with a call to dataService.getMovies()', function() { 
    $httpBackend.expectGET("./src/app/movies/data.json").respond(response); 
    $httpBackend.flush(); 
}); 
+0

나는 그것을 시도했지만 여전히 운이 없다. – user3788267

+0

@ user3788267 편집을 참조하십시오. expectGET URL 끝 부분에 작은 따옴표가 추가로 있습니다. – dwbartz

+0

어리석은 나를. 그러나 나는 여전히 같은 오류가 나타나고 있다고 정정했다. – user3788267