注:私はすべてをCoffeeScriptで書いています
私は次のようなコントローラーを持っています:
angular.module('myApp').controller 'MyController', ($scope, $routeParams, Batch) ->
$scope.$on '$routeChangeSuccess', () ->
Batch.get {batchUuid: $routeParams.batchUuid}, (response) ->
$scope.batch_id = response.id
コントローラは、次のように定義されているバッチリソースを取得します。
angular.module('myApp').factory 'Batch', ($resource) ->
$resource '/batches/:batchUuid', {}
私はこれのためのルートを次のように持っています:
$routeProvider.when('/batches/:batchUuid',
{ templateUrl: 'batches-processing.html', controller: 'MyController' })
それで:
- ルート/batches/22にアクセスします
- MyControllerを使用します
- $routeChangeSuccessをトリガーします
- ルートからbatchUuidをバッチリソースに取得するように呼び出します
- json応答を取得し、$scope.batch_idにIDを割り当てます。
これをテストしたいので、次のように単体テストを行います。
describe 'Controllers', ->
beforeEach module 'myApp'
describe 'MyController', ->
scope = {}
ctrl= {}
beforeEach inject ($rootScope, $controller, Batch, $httpBackend) ->
scope = $rootScope.$new()
ctrl = $controller('MyController', ($scope: scope))
#I imagine I need to use either my injected Batch resource or the httpBackend to fake/expect the ajax call here
it "Gets data from the resource and assigns it to scope", () ->
#How on earth do I test this
$ httpBackend.expect('/ endpoint.json')。respond({id:1})などを使用している人を見たことがありますが、それはできません。私のリソースURLは、ルートのbatchUuidに依存しています。ルート変更をシミュレートし、$ routeChangeSuccessをトリガーし、routeParameterをリソースに渡し、リソースを取得して結果をテストするテストをシミュレート/偽造/実装するにはどうすればよいですか?
私はこれの上に髪を引っ張っています、そして角度のあるドキュメントはそれをカバーしていないようです。
ありがとう!