使用Jest进行角度1.6的单元测试

问题描述 投票:1回答:1

我正在尝试测试使用Marvel API的角度服务,我正在关注this article,但我无法弄清楚我做错了什么。

错误

Failed to instantiate module marvel due to:{1}

我的app.js.

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

我的测试规范代码

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')
      })
  })
})

奇迹服务

(() => {
  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
      }
    })
})()
angularjs jest
1个回答
1
投票

问题正如错误所说的那样。应该有marvel模块,它不存在。

angular.module('marvel')是模块吸气剂。预计该模块已经使用angular.module('marvel', [])定义。

© www.soinside.com 2019 - 2024. All rights reserved.