Angularjs 在angular 1.6中使用Jest进行单元测试
我正在尝试测试我的angular服务,它使用了Marvel API,我遵循了它,但我无法找出我做错了什么 错误Angularjs 在angular 1.6中使用Jest进行单元测试,angularjs,jestjs,Angularjs,Jestjs,我正在尝试测试我的angular服务,它使用了Marvel API,我遵循了它,但我无法找出我做错了什么 错误 Failed to instantiate module marvel due to:{1} 我的app.js (() => { angular.module('marvel', [ 'ui.router', 'ngAnimate', 'ngAria', 'ngMessages', 'ngMaterial' ]) })() 我的
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
}
})
})()
惊奇漫画服务
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
}
})
})()
问题就如错误所说的那样。应该有
marvel
模块,但它不在那里
angular.module('marvel')
是模块getter。预计模块已经用angular.module('marvel',[])定义好了
Good!我根据您的回答更正了代码,它也更正了问题,但现在我遇到了marvel.service的问题,您是否遇到过此问题?请参阅。您没有加载这些模块。我想你和茉莉也会遇到同样的问题。问题是您将在服务单元测试中加载所有这些模块(ui.router等)。正确的方法是使用不具有所有这些依赖关系的子模块。