简体   繁体   English

角茉莉花测试错误-未定义$ injector

[英]Angular Jasmine tests error - $injector is not defined

Using Karma Im back filling tests on a project (someone else wrote the angular app - I have the 'pleasure')... 在项目上使用Karma Im回填测试(其他人编写了angular应用程序-我有“高兴”的意思)...

Project written in coffeescript. 用coffeescript编写的项目。

I have the following jasmine test: 我进行了以下茉莉花测试:

'use strict'

describe 'module:file:controller', () ->
  $controller = null
  $scope = null
  $state = null
  $stateParams = null
  Page = null
  File = null

  beforeEach (module 'forge')

  beforeEach inject(($rootScope, _$controller_, _$state_, _$stateParams_, _Page_, _File_) ->
    $scope = $rootScope.$new()
    $state = _$state_
    $stateParams = _$stateParams_
    Page = _Page_
    File = _File_
    $controller = _$controller_
  )

  describe 'init', () ->
    controller = null

    beforeEach () ->
      $state = {}
      $stateParams = {}
      controller = $controller('fileController', {$scope: $scope, $state: $state, $stateParams: $stateParams, Page: Page, File: File})

    it 'should set page title to File', () ->
      spyOn(Page, 'title')

      expect(Page.title).toHaveBeenCalledWith 'Files'

    it 'should do something with delete', () ->
      spyOn(Page, 'addAlert')

      $scope.delete(1)

      expect(Page.addAlert).toHaveBeenCalled()

Here is the controller: 这是控制器:

forge.controller 'fileController', ($scope, $state, $stateParams, Page, File) ->

  # Inject base controller
  $injector.invoke BaseController, this, { $scope: $scope, Factory: File }

  # Set page title
  Page.title 'Files'

  console.log $stateParams
  console.log $state

  # Delete
  $scope.delete = (id) ->

    Page.addAlert 'success', '[TEST] Deleting file...'

    console.log $stateParams
    console.log $state

  # Delegate actions
  switch $stateParams.action
    when 'delete' then $scope.delete $stateParams.id

I have a bunch of unit test running fine. 我有一堆单元测试运行良好。 The application is built using grunt and all code is compiled into /dist/assets/js/app.js. 该应用程序是使用grunt构建的,所有代码都被编译到/dist/assets/js/app.js中。

My karma config: 我的业力配置:

module.exports = (config) ->
  config.set

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

    preprocessors:
      'unit/**/**.test.coffee': ['coffee']
      '../module/**/test/unit/**/*.unit.coffee': ['coffee']

    frameworks: ['jasmine']

    reporters: ['progress']

    browsers: ['Chrome', 'Firefox', 'Safari']

    logLevel: 'ERROR'

    jasmineNodeOpts:
      onComplete: null
      isVerbose: false
      showColors: true
      includeStackTrace: false
      defaultTimeoutInterval: 30000

When I run the tests I receive: 当我运行测试时,我收到:

Chrome 48.0.2564 (Mac OS X 10.11.3) module:file:controller init should set page title to File FAILED
  ReferenceError: $injector is not defined
  at new <anonymous> (/Users/usr1/repos/proj/dist/assets/js/forge.js:75397:3)
  at Object.instantiate (/Users/usr1/repos/proj/dist/assets/js/forge.js:14449:14)
  at /Users/usr1/repos/proj/dist/assets/js/forge.js:19700:28
  at /Users/usr1/repos/proj/bower_components/angular-mocks/angular-mocks.js:2170:12
  at Object.<anonymous> (/Users/usr1/repos/proj/src/module/file/test/unit/controller/file-controller.unit.js:25:27)
  Expected spy title to have been called with [ 'Files' ] but it was never called.
  at Object.<anonymous> (/Users/usr1/repos/proj/src/module/file/test/unit/controller/file-controller.unit.js:35:33)
Chrome 48.0.2564 (Mac OS X 10.11.3) module:file:controller init should do something with delete FAILED
  ReferenceError: $injector is not defined
  at new <anonymous> (/Users/usr1/repos/proj/dist/assets/js/forge.js:75397:3)
  at Object.instantiate (/Users/usr1/repos/proj/dist/assets/js/forge.js:14449:14)
  at /Users/usr1/repos/proj/dist/assets/js/forge.js:19700:28
  at /Users/usr1/repos/proj/bower_components/angular-mocks/angular-mocks.js:2170:12
  at Object.<anonymous> (/Users/usr1/repos/proj/src/module/file/test/unit/controller/file-controller.unit.js:25:27)
TypeError: $scope.delete is not a function
  at Object.<anonymous> (/Users/usr1/repos/proj/src/module/file/test/unit/controller/file-controller.unit.js:39:23)
Chrome 48.0.2564 (Mac OS X 10.11.3): Executed 63 of 63 (2 FAILED) (1.155 secs / 1.134 secs)

Checked all the paths and that the controllers are presentin teh built app.js file. 检查所有路径,并在内置的app.js文件中显示控制器。

At a loss now as to why this message persists and any pointers would be great. 现在不知为什么该消息仍然存在,并且任何指针都很好。

My application was minified - this would prevent this working. 我的应用程序已缩小-这将阻止此工作。

angular injector docs. 角度喷射器文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ja茉莉角错误:[$ injector:nomod] - Karma Jasmine Angular Error: [$injector:nomod] 使用Karma / Jasmine进行角单元测试-未定义浏览器 - Angular unit tests with Karma/Jasmine - browser not defined 使用Jasmine和PhantomJS进行Angular JS测试,错误为0 - Angular JS tests with Jasmine and PhantomJS, 0 Error 使用业力和茉莉花运行角度测试:minErr错误 - Running angular tests with karma and jasmine: minErr error 0Uncaught ReferenceError:未定义角度,Uncaught错误:[$ injector:modulerr] - 0Uncaught ReferenceError: angular is not defined , Uncaught Error: [$injector:modulerr] arma茉莉角1.6.2测试筛选器引发错误未捕获的错误:[$ injector:unpr] - Karma jasmine Angular 1.6.2 testing filter throws error Uncaught Error: [$injector:unpr] 未定义arma茉莉角 - Karma Jasmine Angular not defined 错误:喷油器已创建,无法注册模块! 角1.2茉莉1.3.0 - Error: Injector already created, can not register a module! Angular 1.2 jasmine 1.3.0 Angular:如何解决$ httpProvider的[$ injector:unpr]错误并在茉莉花测试中注入控制器 - Angular: How to resolve [$injector:unpr] error for $httpProvider and inject controller in jasmine test 我正在尝试使用茉莉花来测试我的角度控制器。 但我收到错误:[$ injector:modulerr] - i am trying to test my angular controller using jasmine. But i am getting Error: [$injector:modulerr]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM