Bra*_*row 7 javascript unit-testing jasmine angularjs
注意:我在CoffeeScript中编写所有内容
我有一个控制器如下:
angular.module('myApp').controller 'MyController', ($scope, $routeParams, Batch) ->
$scope.$on '$routeChangeSuccess', () ->
Batch.get {batchUuid: $routeParams.batchUuid}, (response) ->
$scope.batch_id = response.id
Run Code Online (Sandbox Code Playgroud)
控制器提取Batch资源,该资源定义如下:
angular.module('myApp').factory 'Batch', ($resource) ->
$resource '/batches/:batchUuid', {}
Run Code Online (Sandbox Code Playgroud)
我有一个这样的路线如下:
$routeProvider.when('/batches/:batchUuid',
{ templateUrl: 'batches-processing.html', controller: 'MyController' })
Run Code Online (Sandbox Code Playgroud)
所以:
现在我想测试这个,所以我有一个单元测试如下:
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
Run Code Online (Sandbox Code Playgroud)
我见过人们使用$ httpBackend.expect('/ endpoint.json').respond({id:1})等但我不能这样做.我的资源URL依赖于路由中的batchUuid.如何模拟/伪造/实现将模拟路由更改的测试,触发$ routeChangeSuccess,将routeParameter传递给资源,获取资源并测试结果?
我正在把头发拉过来,角度的文档似乎没有覆盖它.
谢谢!
归档时间: |
|
查看次数: |
5707 次 |
最近记录: |