3

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は[関数]として返されます。

私の質問は:

ジャスミンテストを実行してサービスに正しく入り、応答を取得するにはどうすればよいですか?また、サービスの直接単体テストを作成する方法はありますか?

ありとあらゆる助けをいただければ幸いです

4

1 に答える 1

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 エンドポイントは他の場所で広範囲にテストされています。

于 2012-11-08T09:14:18.960 に答える