2

尝试开始在 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})

有任何想法吗?

4

1 回答 1

5

更改此行

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

到以下

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

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

于 2012-12-05T00:07:16.980 回答