1

I'm trying to test my angular service that uses the Marvel API, and I was following this article, but I can't figure it out what I'm doing wrong.

Error

Failed to instantiate module marvel due to:{1}

My app.js

(() => {
  angular.module('marvel', [
    'ui.router',
    'ngAnimate',
    'ngAria',
    'ngMessages',
    'ngMaterial'
  ])
})()

My Test Spec code

require('../../node_modules/angular/angular.min.js')
require('../../node_modules/angular-mocks/angular-mocks.js');
require('../../src/app/app.js')
require('../../src/app/services/marvel.service.js')

describe('\nFail Cases', () => {
  beforeEach(angular.mock.module('marvel'))
  let _marvelservice
  beforeEach(inject((MarvelService) => {
    _marvelservice = MarvelService
  }));

  test('should return false when user do not put the id for details correctly', (done) => {
    _marvelservice.getDetail()
      .catch((err) => {
        expect(err.xhrStatus).toBe('error')
      })
  })
})

Marvel Service

(() => {
  angular.module('marvel')
    .factory('MarvelService', ($http, $q, Config) => {
      /**
       * Get 10 characters from Marvel API.
       * @return {Object} Doc with all character recovered.
       */
      function getCharacters () {
        return request('', 'GET', { ts: 1, limit: 10, apikey: 
       `${Config.MARVEL.PUBLIC_KEY}`, hash: `${Config.MARVEL.MD5}` })
      }

      /**
       * Get details from a characters by consulting the Marvel API.
       * @return {Object} Doc with detail character recovered.
       */
      function getDetail (id) {
        const urlAddress = `/${id}`
        return request(urlAddress, 'GET', { ts: 1, apikey: 
        `${Config.MARVEL.PUBLIC_KEY}`, hash: `${Config.MARVEL.MD5}` })
      }

      /**
       * Responsible for request.
       * @return {Object} Doc with the returned promise.
       */
      function request (path, method, querystring) {
         const options = {
          method,
          url: `${Config.MARVEL.URL}${path}`,
          params: querystring
      }

      return $http(options)
        .then(success => { return success.data }, (err) => {
          return err
        })
      }

      return {
        getCharacters,
        getDetail
      }
    })
})()

1 Answer 1

1

The problem is exactly as the error says. There should be marvel module, and it's not there.

angular.module('marvel') is module getter. It's expected that the module has been already defined with angular.module('marvel', []).

2
  • Good! I correct the code following your answer, and it correct the problem, but now I have a problema with the marvel.service,have you faced this problem ? Commented Feb 28, 2018 at 17:55
  • See stackoverflow.com/a/25582595/3731501 . You don't have these modules loaded. I think you would hit same problems with Jasmine as well. The problem is that you will have all these modules (ui.router, etc) be loaded in service unit test. The proper approach is to have sub-modules that won't have all these dependencies. Commented Feb 28, 2018 at 18:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.