Nota: Sto scrivendo tutto in CoffeeScript
Ho un controller come segue:
angular.module('myApp').controller 'MyController', ($scope, $routeParams, Batch) ->
$scope.$on '$routeChangeSuccess',() ->
Batch.get {batchUuid: $routeParams.batchUuid}, (response) ->
$scope.batch_id = response.id
Il controller tira nella risorsa batch che è definiti come segue:
angular.module('myApp').factory 'Batch', ($resource) ->
$resource '/batches/:batchUuid', {}
ho un percorso per questo come segue:
$routeProvider.when('/batches/:batchUuid',
{ templateUrl: 'batches-processing.html', controller: 'MyController' })
Quindi:
- di visitare il percorso /lotti/22
- Esso utilizza MyController
- Si innesca $ routeChangeSuccess
- Chiede salire sul strega risorsa Batch il batchUuid dalla route
- Torna una risposta json e assi GNS un id a $ scope.batch_id
Ora voglio provare questo quindi ho un test di unità come segue:
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
Ho visto persone che utilizzano $ httpBackend.expect ('/ endpoint. json '). rispondi ({id: 1}) ecc. ma non posso farlo. L'URL della mia risorsa si basa su un batchUuid dalla rotta. Come posso simulare/falsificare/implementare un test che simulerà una modifica del percorso, attivare $ routeChangeSuccess, passare un parametro Percorso alla risorsa, eseguire un get sulla risorsa e testare il risultato?
Mi sto tirando i capelli e questo documento angolare non sembra coprirlo.
Grazie!
compagno Legendary. Semplicemente leggendario. –
meno uno per coffeescript? boo. La domanda stessa è nel coffeescript –