当我尝试传递 json 对象进行测试时,Mocha 测试得到 "getting 500 Unexpected JSON o in JSON at position 1"
Mocha Testing Getting "getting 500 Unexpected JSON o in JSON at position 1" when I try to pass json object for test
在位置 1 JSON 中得到 500 个意外的 JSON o。
- 我试过从 json 键中删除引号,它适用于 postman 但在 mocha 中失败。
- 也尝试使用 JSON.stringify 发送数据,解析 json 数据时出现一些问题。
- 尝试使用 mocha .type('json') 但没有结果。
我的摩卡测试如下:-
const request = require('supertest');
const app = require('../../../../index');
const httpStatus = require('http-status');
const User = require('../../../models/auth/user.model');
const bcrypt = require('bcryptjs');
const appModel = require('../../../models/app/apps.model');
const RefreshToken = require('../../../models/auth/refreshToken.model');
describe('API', async () => {
let appParam;
let validApiParam;
let userAccessToken;
let Ace;
const password = '123456';
beforeEach(async () => {
const passwordHashed = await bcrypt.hash(password, 1);
Ace = {
email: 'myMail@gmail.com',
password: passwordHashed,
name: 'ace',
role: 'user',
};
await User.deleteMany({});
await appModel.deleteMany({});
await RefreshToken.deleteMany({});
await User.insertMany([Ace]);
Ace = await User.findOne({ name: Ace.name }).exec();
appParam = {
name: 'aegis project',
appDescription: 'It is authentication app',
userId: Ace._id.toString(),
};
appParam = await appModel.create(appParam);
validApiParam = {
name: 'validNewApi',
url: 'testurl',
methodName: 'POST',
apiType: 2,
requestParameter: {
'email': 'eve.holt@reqres.in',
'password': 'cityslicka',
},
responseParameter: {
'version': '1',
'user': {
'id': 123,
},
},
userIdKeyPath: 'user.id',
serviceProvider: 'axios',
userId: Ace._id.toString(),
appId: appParam._id.toString(),
};
Ace.password = password;
userAccessToken = (await User.findAndGenerateToken(Ace)).accessToken;
});
describe('POST /apiRegistration/save-login-api/save-login-api', () => {
it('should create a new login Api', () => {
return request(app)
.post('/apiRegistration/save-login-api/save-login-api')
.set('Authorization', `Bearer ${userAccessToken}`)
.send(JSON.stringify(validApiParam))
.expect(httpStatus.CREATED)
.then(() => {
});
});
});
});
提供的任何建议都会有所帮助。
发现我正在解析的解决方案已经 JSON 格式化请求参数在我的逻辑中导致了这个错误。
在位置 1 JSON 中得到 500 个意外的 JSON o。
- 我试过从 json 键中删除引号,它适用于 postman 但在 mocha 中失败。
- 也尝试使用 JSON.stringify 发送数据,解析 json 数据时出现一些问题。
- 尝试使用 mocha .type('json') 但没有结果。 我的摩卡测试如下:-
const request = require('supertest');
const app = require('../../../../index');
const httpStatus = require('http-status');
const User = require('../../../models/auth/user.model');
const bcrypt = require('bcryptjs');
const appModel = require('../../../models/app/apps.model');
const RefreshToken = require('../../../models/auth/refreshToken.model');
describe('API', async () => {
let appParam;
let validApiParam;
let userAccessToken;
let Ace;
const password = '123456';
beforeEach(async () => {
const passwordHashed = await bcrypt.hash(password, 1);
Ace = {
email: 'myMail@gmail.com',
password: passwordHashed,
name: 'ace',
role: 'user',
};
await User.deleteMany({});
await appModel.deleteMany({});
await RefreshToken.deleteMany({});
await User.insertMany([Ace]);
Ace = await User.findOne({ name: Ace.name }).exec();
appParam = {
name: 'aegis project',
appDescription: 'It is authentication app',
userId: Ace._id.toString(),
};
appParam = await appModel.create(appParam);
validApiParam = {
name: 'validNewApi',
url: 'testurl',
methodName: 'POST',
apiType: 2,
requestParameter: {
'email': 'eve.holt@reqres.in',
'password': 'cityslicka',
},
responseParameter: {
'version': '1',
'user': {
'id': 123,
},
},
userIdKeyPath: 'user.id',
serviceProvider: 'axios',
userId: Ace._id.toString(),
appId: appParam._id.toString(),
};
Ace.password = password;
userAccessToken = (await User.findAndGenerateToken(Ace)).accessToken;
});
describe('POST /apiRegistration/save-login-api/save-login-api', () => {
it('should create a new login Api', () => {
return request(app)
.post('/apiRegistration/save-login-api/save-login-api')
.set('Authorization', `Bearer ${userAccessToken}`)
.send(JSON.stringify(validApiParam))
.expect(httpStatus.CREATED)
.then(() => {
});
});
});
});
提供的任何建议都会有所帮助。
发现我正在解析的解决方案已经 JSON 格式化请求参数在我的逻辑中导致了这个错误。