如何测试依赖于ngResource服务的Controller

时间:2012-11-07 01:40:09

标签: coffeescript angularjs jasmine

我有一个控制器,它依赖于通过ngResource构建的服务。我在测试时遇到了麻烦(尽管两者在实际应用中似乎都很有魅力)。以下是(已消毒的)控制器

MyApp.Controller.MyCarsController = (scope, http, typeService) ->
  if scope.context==undefined
    scope.ferrari_or_porshe =""
    scope.id = ""
  else if scope.context=="ferrari"
    scope.country_or_pi ="Ferrari"
  else if scope.context=="porshe"
    scope.country_or_pi ="Porshe"

  typeService.index
    ferrari_or_porshe: scope.ferrari_or_porshe
    id: scope.id
  , (response) ->
    scope.type = response
    scope.loading = false

MyApp.Controller.MyCarsController.$inject = ['$scope', '$http', 'Type']

这就是服务:

MyApp.MyModule.factory 'Type', ['$resource', ($resource) ->
  TypeResource = $resource("/api/types/:ferrari_or_porshe/:id", {},
    index:
      method: "GET"
      isArray: true
  )
  return TypeResource
]

最后,一些测试代码:

describe 'MyApp.Controller.MyCarsController', ->
  beforeEach module('MyModule')
  beforeEach inject ($rootScope, $http, $controller, Type) ->
    @scope = $rootScope.$new()
    @typeService = Type
    @scope.context = undefined
    $controller 'MyApp.Controller.MyCarsController', $scope: @scope

  describe '#home-page', ->

    it 'contains a list of types', ->
      expect(@scope.types.length).toBeGreaterThan 0

    it "sets instance variables correctly", ->
      expect(@scope.ferrari_or_porshe).toBe ""
      expect(@scope.id).toBe ""

哪个失败了:

  No more request expected in helpers/angular-mocks.js on line 889
  TypeError: 'undefined' is not an object (evaluating 'this.scope.types.length') in controllers/my_cars_controller_spec.js

通过明智地应用console.logs,我发现问题是永远不会达到响应的最终回调。 TypeResource返回[Function]。

我的问题是:

如何驱动Jasmine测试以正确进入服务并获取响应?有没有办法为服务创建直接的单元测试?

感谢任何和所有帮助

1 个答案:

答案 0 :(得分:5)

解决方案如下:对于服务,使用捆绑为ngMock的$ httpBackend:

http://code.google.com/p/google-drive-sdk-samples/source/browse/ruby/app/lib/angular-1.0.0/angular-mocks-1.0.0.js?r=c43c943e32be395b7abca8150deb301d3cbc0dbe

使用此方法模拟Rest响应。因为在我的情况下,我只关心验证GET请求是否消失:

 describe 'Type', ->
   describe '#index', ->
     beforeEach module('MyModule')
     beforeEach inject(($httpBackend) ->
       $httpBackend.whenGET('/api/types/ferrari/1').respond([])
     )

     it 'for a ferrari scope', inject((Type) ->
       ferrari_or_porsche = 'ferrari'
       id = '1'
       expect( Type.index('ferrari_or_porsche': ferrari_or_porsche, 'id': id) ).toEqual([ ])
     )

然后对于控制器,使用jasmine间谍模拟服务并使用jasmine.any(Function)来警告回调。

 describe 'MyApp.Controller.MyCarsController', ->
   beforeEach module('myModule')
   beforeEach inject ($rootScope, $http, $controller, Type) ->
     @scope = $rootScope.$new()
     @typeService = Type
     @scope.context = undefined
     spyOn(@typeService, 'index')

   describe '#home-page', ->
     beforeEach inject ($controller) ->
       $controller 'MyApp.Controller.MyCarsController', $scope: @scope
     it 'contains a list of types', ->
       expect(@typeService.index).toHaveBeenCalledWith({ ferrari_or_porsche : '', id : '' }, jasmine.any(Function))

     it "sets instance variables correctly", ->
       expect(@scope.ferrari_or_porsche).toBe ""
       expect(@scope.id).toBe ""

注意:我对此解决方案的“规范性”没有任何说法。但它的确有效。 注意:API端点当然在其他地方进行了广泛测试。