3

I am trying to write an angularjs spec, but the beforeEach Inject block is not being executed, leaving my test services undefined

describe 'VodAssetsTriageController', ->
  $charterModal = null
  controller = null
  FieldService = null
  $q = null
  $rootScope = null
  $scope = null
  VodAssetsService = null
  PidService =
    getPids: _.noop
  AdiSearchService =
    getHeadEnds: _.noop

  beforeEach module('alphonso', ($provide) ->
    $provide.factory 'AdiSearchService',
      $get: -> AdiSearchService
    $provide.factory 'PidService',
      $get: -> PidService
  )


  beforeEach inject ($controller, _VodAssetsService_, _VodAssetsFieldService_,            _$charterModal_, _$q_, _$rootScope_, _AdiSearchService_, _PidService_) ->

     controller = $controller
     $charterModal = _$charterModal_
     VodAssetsService = _VodAssetsService_
     FieldService = _VodAssetsFieldService_
     $q = _$q_
     $rootScope = _$rootScope_
     $scope = $rootScope.$new()
     AdiSearchService = _AdiSearchService_
     PidService = _PidService_

     result =
      content: []
     spyOn PidService, 'getPids'
      .and.returnValue $q.when(result)
     spyOn AdiSearchService, 'getHeadEnds'
      .and.returnValue $q.when(result)

     controller = controller 'VodAssetsTriageCtrl',
      VodAssetsService: VodAssetsService,
      VodAssetsFieldService: FieldService,
      $scope: $scope,
      $charterModal: $charterModal

     installPromiseMatchers()

  describe 'controller.paginate.onChange()', ->
    result = null
    searchDeferred = null

    beforeEach ->
      page =
        size: 1000
        totalElements: 179000
      response =
        content: []
        number: 0
        page: page
      result =
       response: response
      searchDeferred = $q.defer()
      spyOn VodAssetsService, 'getAll'
        .and.returnValue searchDeferred.promise
      $scope.searchVm =
        doSearch: () -> controller.search()

    it 'should invoke controller.paginate.onChange() on search() invocation', ->
      controller.search controller
      expect controller.paginate.loading
       .toBeTruthy()

      spyOn controller.paginate, 'onChange'
      promise = searchDeferred.resolve(result)
      $rootScope.$apply()

      expect promise
        .toBeResolvedWith response
      expect vm.paginate.totalElements
        .toBe 179
      expect controller.page
        .toBe result.response.page.number
      expect controller.size
        .toBe result.response.page.size
      expect controller.paginate.loading
        .toBeFalsy()
      expect controller.paginate.onChange
        .toHaveBeenCalled()

    it 'should page to vm.paginate.page = 1', ->
      controller.paginate.page = 1
      controller.paginate.cache[0] = _.range 0, 1000
      spyOn $scope.searchVm, 'doSearch'
      controller.paginate.onChange()

      expect vm.page
        .toBe 1
      expect $scope.searchVm.doSearch
        .not.toHaveBeenCalled()
      expect controller.vodAssets.length
        .toBe vm.paginate.size

When I execute the code I am getting the following errors

Blockquote Error: [ng:areq] Argument 'fn' is not a function, got Object http://errors.angularjs.org/1.4.8/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20Object in /Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js (line 1815) assertArg@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:1815:91 assertArgFn@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:1825:12 annotate@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:3810:16 $$annotate@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular-mocks/angular-mocks.js:2222:41 invoke@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:4501:46 /Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:4344:85 forEach@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:340:24 createInjector@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular/angular.js:4344:10 workFn@/Users/rbrooks3/sandbox/alphonso-web/bower_components/angular-mocks/angular-mocks.js:2428:60 TypeError: null is not an object (evaluating '$q.defer') in /Users/rbrooks3/sandbox/alphonso-web/src/app/components/vod-assets-triage/vod-assets-triage.spec.js (line 34) /Users/rbrooks3/sandbox/alphonso-web/src/app/components/vod-assets-triage/vod-assets-triage.spec.js:34:15

Does anyone know why the beforeEach inject function is not being called?

1 Answer 1

0

Here,

  beforeEach(module('alphonso', function($provide) {
    $provide.factory('AdiSearchService', {
      $get: function() {
        return AdiSearchService;
      }
    });
    return $provide.factory('PidService', {
      $get: function() {
        return PidService;
      }
    });
  }));

module config function shouldn't return anything, and it returns an object.

Also, it obviously should be $provide.provider and not $provide.factory, short syntax for service mocking is

  beforeEach(module('alphonso', function($provide) {
    $provide.factory('AdiSearchService', {
      AdiSearchService: AdiSearchService,
      PidService: PidService
    });
  }));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.