模拟AngularJS和Jasmine的问题

时间:2012-12-04 23:52:23

标签: angularjs jasmine

尝试'开始在angularjs中进行测试(基于this),但似乎无法使模拟正常工作。这是我的茉莉花文件:

describe 'FlightsController', ->
  beforeEach module 'surf-air'

  beforeEach inject ($rootScope, $controller) ->
    scope = $rootScope.$new()
    FlightsController = $controller('FlightsController', {scope: scope})

  it 'should work', -> expect(true).toBe(true)

这就是Testacular吐出来的:

Chrome 23.0 FlightsController should work FAILED
    Error: Unknown provider: $scopeProvider <- $scope
        at Error (<anonymous>)
        at /Users/chriskun/forge-workspace/surf-air-pilot-mobile/src/js/angular.min.js:28:395
        at Object.c [as get] (/Users/chriskun/forge-workspace/surf-air-pilot-mobile/src/js/angular.min.js:26:180)
        at /Users/chriskun/forge-workspace/surf-air-pilot-mobile/src/js/angular.min.js:28:476
        at c (/Users/chriskun/forge-workspace/surf-air-pilot-mobile/src/js/angular.min.js:26:180)
        at d (/Users/chriskun/forge-workspace/surf-air-pilot-mobile/src/js/angular.min.js:26:314)
        at Object.instantiate (/Users/chriskun/forge-workspace/surf-air-pilot-mobile/src/js/angular.min.js:27:455)
        at /Users/chriskun/forge-workspace/surf-air-pilot-mobile/src/js/angular.min.js:50:239
        at null.<anonymous> (/Users/chriskun/forge-workspace/surf-air-pilot-mobile/test/unit/controllers.js:9:34)
        at Object.d [as invoke] (/Users/chriskun/forge-workspace/surf-air-pilot-mobile/src/js/angular.min.js:27:325)
    Error: Declaration Location
        at window.jasmine.window.inject.angular.mock.inject (/Users/chriskun/forge-workspace/surf-air-pilot-mobile/test/lib/js/angular-mocks.js:1717:25)
        at null.<anonymous> (/Users/chriskun/forge-workspace/surf-air-pilot-mobile/test/unit/controllers.js:6:16)
        at /Users/chriskun/forge-workspace/surf-air-pilot-mobile/test/unit/controllers.js:4:3
        at /Users/chriskun/forge-workspace/surf-air-pilot-mobile/test/unit/controllers.js:18:4
Chrome 23.0: Executed 1 of 1 (1 FAILED) (0.221 secs / 0.04 secs)

由以下原因引起:

$controller('FlightsController', {scope: scope})

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

更改此行

FlightsController = $controller('FlightsController', {scope: scope})

以下

FlightsController = $controller('FlightsController', {$scope: scope})

我已将示例jsfiddle更新为CoffeeScript:http://jsfiddle.net/jaimem/5Afbm/2/