我正在使用nock伪造GraphQL服务器的响应,但是似乎我的nock代码没有受到尊重,而且我似乎不太明白为什么。
下面是一大堆代码,但是似乎当我输出函数调用的结果时,数据不是我期望的。
在以下测试中:分别获取每个发行版ID ,它两次调用nock,但是我期望输出如下:
[ { id: 123456, asset_controller: { id: 54321 }, display_author: 'Jason', author: [ [Object] ] }, { id: 78902, asset_controller: { id: 54321 }, display_author: 'King', author: [ [Object] ] } ]
我取回了两个完全相同的 Jason 对象。
在以下测试中:当发布不可用时返回包含错误的预期结果,我应该得到一个包含错误的数组,但是我得到了一个 Jason < / strong>返回。
我想知道在我的beforeEach
中是否存有Nock响应,是否在每个测试中都覆盖了nock响应?删除beforeEach
中的nock响应会导致错误。
const Releases = require('../../../src/services/Releases');
const authorisation = require('../../../src/services/Authorisation');
const ServiceDiscovery = require('service-discovery');
const Lock = require('../../../src/helpers/Lock');
const http = require('http');
describe('Releases', () => {
let serviceDiscovery;
const address = '192.0.0.1';
const port = 4002;
const url = 'http://' + address + ':' + port;
const token = 'abcd';
beforeEach(() => {
sinon.stub(authorisation, 'authorise').resolves(
{
token: token,
expiresAt: 12344657547
}
);
authorisation.token = token;
serviceDiscovery = sinon.createStubInstance(ServiceDiscovery);
serviceDiscovery.discoverServiceDetails.resolves(
[
{
Address: address,
ServicePort: port,
},
]
);
nock(url, {
reqheaders: {
'authorization': 'Bearer ' + token,
'content-type': 'application/json',
},
})
.post('/graphql')
.reply(200, {
data: {
ReleaseFormat: [
{
id: 123456,
asset_controller: {
id: 54321,
},
display_author: 'Jason',
author: [
{
id: 123456,
name: 'jason',
},
],
}
]
}
});
});
afterEach(() => {
authorisation.authorise.restore();
});
describe('getReleases', () => {
it('retrieves each release id individually', async () => {
const releaseIDs = [123456, 78902];
const releases = new Releases(authorisation, http, serviceDiscovery, Lock);
serviceDiscovery.discoverServiceDetails.resolves(
[
{
Address: address,
ServicePort: port,
alive: true,
},
]
);
nock(url, {
reqheaders: {
'Authorization': 'Bearer ' + token,
'content-type': 'application/json',
},
})
.post('/graphql', JSON.stringify({
'query': `{
ReleaseFormat(qbe: {
id: 123456
asset_controller: {
id: 57753805
}
})
{
id,
asset_controller {
id,
name
},
display_author,
author {
id,
name,
ISNI
},
}
}`
}))
.reply(200, {
data: {
ReleaseFormat: [
{
id: 123456,
asset_controller: {
id: 54321,
},
display_author: 'Jason',
author: [
{
id: 123456,
name: 'jason',
},
],
}
]
}
})
.post('/graphql', JSON.stringify({
'query': `{
ReleaseFormat(qbe: {
id: 78902
asset_controller: {
id: 57753805
}
})
{
id,
asset_controller {
id,
name
},
display_author,
author {
id,
name,
},
}
}`
}))
.reply(200, {
data: {
ReleaseFormat: [
{
id: 78902,
asset_controller: {
id: 54321,
},
display_author: 'King',
author: [
{
id: 8764567,
name: 'king',
},
],
}
]
}
});
const spy = sinon.spy(releases, '_getReleaseData');
const actual = await releases.getReleases(releaseIDs);
console.log(actual);
expect(spy.called).to.eql(true);
expect(spy.callCount).to.eql(releaseIDs.length);
spy.restore();
});
it('returns an expected result containing errors when the release is not available', async () => {
const releaseIDs = [123456];
const releases = new Releases(authorisation, http, serviceDiscovery, Lock);
serviceDiscovery.discoverServiceDetails.resolves(
[
{
Address: address,
ServicePort: port,
alive: true,
},
]
);
nock(url, {
reqheaders: {
'authorization': 'Bearer ' + token,
'content-type': 'application/json',
},
})
.post('/graphql', JSON.stringify({
'query': `{
ReleaseFormat(qbe: {
id: 123456
asset_controller: {
id: 57753805
}
})
{
id,
asset_controller {
id,
name
},
display_author,
author {
id,
name,
},
}
}`
}))
.reply(200, {
data: {
ReleaseFormat: []
}
});
const expected = [
new Error(`Not a valid release for ID: ${releaseIDs[0]}`)
];
const actual = await releases.getReleases(releaseIDs);
console.log(actual);
expect(actual).to.be.an('array');
expect(actual.length).to.be.eql(expected.length);
expect(actual.filter(e => e instanceof Error).length).to.be.eql(1);
});
});
});