我想测试一些 graphql 突变。但我不确定在编写测试时要给出什么作为上下文。例如,我想调用 createPost 突变:
it('admin should create a post', async () => {
await createPost(
null,
{
title: 'How to write a blog post',
body: 'lorem ipsum dollar emmet',
published: true,
},
dummyContext
).should.be.fulfilled;
});
这里作为一个 dummyContext 我正在使用这个:
const dummyContext = {
request: { get: () => token },
};
但是,它不起作用。内部createPost
变异logincheker
方法称为:
export async function loginChecker({ request }) {
const Authorization = request.get('Authorization');
if (Authorization) {
const token = Authorization.replace('Bearer ', '');
const userInJwt = jwtValidator(token);
const user = await prisma.user.findOne({ where: { id: userInJwt.id } });
if (!user) {
throw new Error('Not Authorized');
}
return user;
}
}
我在运行测试用例时遇到了这个错误
AssertionError: expected promise to be fulfilled but it was rejected with 'TypeError: Authorization.replace is not a function