2014-10-17 43 views
0

在我的控制器中,我使用$ resource:ng.resource.IResourceService,可以帮助我从服务器获取数据。

controllers.controller("testController", [ 
     "$scope", "$resource", "$rootScope", "$http", 
     "$window", "$location", "resourceService", 
     function ($scope, $resource: ng.resource.IResourceService, $rootScope: IRootScope, 
        $http, $window, $location, resourceService: services.IResourceService) { ...//implementation 
    }]); 

这里是我的茉莉花单元测试的尝试。然而,我越来越 错误:[$注射器:unpr]未知的提供者:$ resourceProvider < - $ resource
你将如何模拟$ resourceProvider?

describe("testController Tests", function(): void { 
     var vm: createDailylog.IViewModel; 
     var $scope: ng.IScope; 
     var $rootScope; 
     var $httpBackend: ng.IHttpBackendService; 
     //var $injector = angular.injector(['ng', 'ngResource']); 
     var $resource = $injector.get('$resource'); 

     beforeEach(function(): void { 
      module("controllers"); 
     }); 

     beforeEach(inject(function (_$controller_: ng.IControllerService, 
      _$httpBackend_, _$resource_, _$rootScope_: IRootScope, $injector) { 

      $httpBackend = _$httpBackend_; 
      //$resource = _$resource_; 

      this.$httpBackend = $httpBackend; 

      $rootScope = _$rootScope_.$new(); 

      $rootScope.config = { 
       serverUrl: "https://test.domainName.net/", 
       serverVersion: "test", 
       title: "Test Server Reference" 
      }; 


      //instantiating controller 
      vm = _$controller_('testController', { 
       $scope: $scope, 
       $resource: $injector.get("$resource"), 
       $rootScope: _$rootScope_, 
       $window: {}, 
       $location: {} 
      }); 
     })); 

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

     describe('when populate method is called', function() { 

      it("should create controller!", function() { 

       $httpBackend.flush(); 
       expect(true).toBe(true); 
      }); 
     }); 

     ); 
    }); 
}); 

回答

0

为什么你需要模拟$resource?你应该只是嘲笑来自$http的数据;你似乎已经这样做了。