我的单元测试为什么不运行?

时间:2014-05-12 13:53:35

标签: angularjs unit-testing mocha karma-runner

我的karma.conf.coffee是:

module.exports = (config) ->
  config.set
    basePath: '../../'

    preprocessors:
      '**/*.coffee': ['coffee']

    files: [
      'dist/assets/vendor.js'
      'public/bower_components/angular-mocks/angular-mocks.js'
      'dist/assets/app.js'
      'test/public/**/*.coffee'
    ]

    singleRun: true

    frameworks: ['mocha']

    browsers: ['PhantomJS']

    reporters: ['story']

    logLevel: config.LOG_INFO

    coffeePreprocessor:
      options:
        bare: true
        sourceMap: false
      transformPath: (path) ->
        path.replace(/\.coffee$/, '.js')

vendor.jsangular.jsangular-route.jsangular-strap.js的串联js。 app.js是我连接的所有角度代码。

我有一个测试:test/public/controllers/login.spec.coffee看起来像:

(->
  describe 'Login Controller', ->
    beforeEach module 'myApp

    it 'should initialize the controller', ->
      # createController()

      # $scope.init()
)()

当我进行测试时,我得到:

PhantomJS 1.9.7 (Mac OS X) - Login Controller:
PhantomJS 1.9.7 (Mac OS X)  "before each" hook: [object Object] FAILED
PhantomJS 1.9.7 (Mac OS X): Executed 1 of 1 (1 FAILED) ERROR (0.005 secs / 0 secs)
PhantomJS 1.9.7 (Mac OS X)
PhantomJS 1.9.7 (Mac OS X) Login Controller "before each" hook: [object Object] FAILED
    TypeError: 'undefined' is not an object (evaluating 'fn.call')

我做错了什么?

2 个答案:

答案 0 :(得分:2)

看起来你错过了收尾报价...

(->
  describe 'Login Controller', ->
    beforeEach module 'myApp' // <------ here you forgot it

    it 'should initialize the controller', ->
      # createController()

      # $scope.init()
)()

答案 1 :(得分:1)

(->
  describe 'Login Controller', ->
    beforeEach module 'myApp

    it 'should initialize the controller', ->
      # createController()

      # $scope.init()
)()

你错过了'after myApp

    beforeEach module 'myApp'

始终预览您的coffeescript