Angular.js单元测试服务对象

时间:2016-01-25 10:39:48

标签: angularjs unit-testing jasmine

在我们的角度控制器中,我们有:

vm = this
vm.wizardService = WizardService # injected service
pageAndFilter.AccountingRun = vm.wizardService.getAccountingRun()._id

我如何模仿vm.wizardService?

我有这个,但它不起作用:

describe 'Controller: Accounting', ->

  beforeEach angular.mock.module('myApp')

  mockScope           = undefined
  $http               = undefined
  $httpBackend        = undefined
  $rootScope          = undefined
  $state              = undefined
  WizardService       = undefined
  vm                  = undefined

  beforeEach angular.mock.inject(($controller, _$rootScope_, _$http_, _$httpBackend_, _$state_, _WizardService_, _vm_) ->

    mockScope          = _$rootScope_.$new()
    $http              = _$http_
    $httpBackend       = _$httpBackend_
    $rootScope         = _$rootScope_
    $state             = _$state_
    vm                 = _vm_
    WizardService      = _WizardService_

    AbrechnungController = $controller('AbrechnungController',
      $scope: mockScope, $httpBackend: $httpBackend, $rootScope:$rootScope, $state:$state
    )
  )

  beforeEach ->
    WizardServiceMock = () ->
      getAccountingRun: () ->
        return {
          '_id':123
        }
    spyOn(WizardService, 'getAccountingRun').and.callFake(WizardServiceMock)

  describe 'Scope Variables and Methods', ->
    it 'should have scope vars and functions defined', (done) ->
      vm.wizardService = WizardService
      expect(vm.wizardService).toBeDefined()
      done()

0 个答案:

没有答案