サービスを利用するディレクティブがあります。
MyDirective = (myService) ->
compile: () ->
stuff = myService.getStuff()
MyDirective.$inject = ['MyService']
app = A.module 'myApp'
app.directive 'myDirective', MyDirective
myService.getStuff が呼び出されたことをテストできるようにしたいと考えています。
コントローラーのテストでは、次のことができました。
angular.mock.inject ($controller) ->
myController = $controller 'MyController',
$scope: $scope
mockMe: mockMethod: () ->
assert.ok()
done()
ディレクティブで同様のことを行うと、次のエラーが発生します。
angular.mock.inject ($directive) ->
myDirective = $directive 'MyDirective',
$scope: $scope
myService: getStuff: () ->
assert.ok()
done()
Error: Unknown provider: $directiveProvider <- $directive
ディレクティブへの参照を取得してモックを挿入する方法はありますか?
私が行っているすべてのテストの前に:
angular.mock.module 'myApp'
ディレクティブ -> サービスではなく、ディレクティブ -> コントローラー -> サービスにすることで、これを解決できました。それは推奨される解決策ですか?例えば
$scope.getStuff = (name, success) ->
assert.ok true
done()
$_compile(elm) $scope
$scope.$digest()