嵌套描述块中的Jasmine间谍未设置

时间:2016-02-12 16:58:27

标签: angularjs coffeescript jasmine

我有以下茉莉花测试脚本......

'use strict'

describe 'module:login:controller', () ->
  $controller = null
  $rootScope = null
  $state = null
  $scope = null
  User = null
  Page = null
  controller = null

  beforeEach module 'forge'

  beforeEach inject((_$controller_, _$rootScope_, _$state_, _User_, _Page_) ->
    $controller = _$controller_
    $rootScope = _$rootScope_
    $state = _$state_
    User = _User_
    Page = _Page_

    $state =
      go: () ->
        return

    $scope = {}

    spyOn(User,'isAuthenticated')
    spyOn($state, 'go')
    spyOn(User,'resetData')
    spyOn(Page,'addAlert')
  )

  it 'should check true login status and redirect to home', () ->
    User.isAuthenticated.and.returnValue true
    controller = $controller 'loginController', {$rootScope:$rootScope, $scope: $scope, $state: $state, User: User, Page: Page}
    expect($state.go).toHaveBeenCalledWith 'home'

  it 'should check false login status and setup scope', () ->
    User.isAuthenticated.and.returnValue false

    controller = $controller 'loginController', {$rootScope:$rootScope, $scope: $scope, $state: $state, User: User, Page: Page}
    expect($state.go).not.toHaveBeenCalledWith 'home'
    expect(User.resetData).toHaveBeenCalled

    expectedScope =
      email: 'robot@site.com'
      password: 'password'
      store: null
      stores: []

    expect($scope.email).toEqual expectedScope.email
    expect($scope.password).toEqual expectedScope.password
    expect($scope.store).toEqual expectedScope.store
    expect($scope.stores).toEqual expectedScope.stores

  describe 'method calls', () ->
    beforeEach User.isAuthenticated.and.returnValue false

    it 'should add Alert if email is empty', () ->
      spyOn(Page,'addAlert')

      controller = $controller 'loginController', {$rootScope: $rootScope, $scope: $scope, $state: $state, User: User, Page: Page}

      $scope.email = ''

      $scope.login()

      expect(Page.addAlert).toHaveBeenCalledWith 'danger', 'Missing Email'

我正在尝试在测试中重复使用间谍,并在需要时更新期望。

运行测试时,错误输出显示为:

Chrome 48.0.2564 (Mac OS X 10.11.3) module:login:controller method calls encountered a declaration exception FAILED
  TypeError: Cannot read property 'isAuthenticated' of null
    at Suite.<anonymous> (/Users/usr/proj/src/module/login/test/unit/controller/login.unit.js:62:20)
    at Suite.<anonymous> (/Users/proj/src/module/login/test/unit/controller/login.unit.js:61:10)
    at /Users/ianwood/Documents/repos/moltin/forge/src/module/login/test/unit/controller/login.unit.js:2:1
Chrome 48.0.2564 (Mac OS X 10.11.3): Executed 65 of 65 (1 FAILED) (1.171 secs / 1.148 secs)

建议用户为空。

我注意到sub描述是在之前执行的 - 所以外部描述测试是最后运行的,但是想要了解如何跨子描述块共享。

0 个答案:

没有答案