尝试使用 Should、Mocha 和 Supertest 读取 JSON 数组时出错
Error when trying to read JSON array using Should, Mocha, & Supertest
我有以下 JSON
负载:
"app": {
"name": "myapp",
"version": "1.0.0",
"last_commit": {
"author_name": "Jon Snow"
"author_email": "my@email.com"
}
}
和以下 .js
文件(使用 Mocha
、Supertest
和 Should
):
var supertest = require('supertest')
var should = require('should')
var server = supertest.agent('http://localhost:3001')
describe('GET /', function () {
it('should respond with JSON', function (done) {
server
.get('/')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
var payload = res.body.app;
payload.should.have.property("app");
payload.should.have.property("name");
payload.should.have.property("version");
payload.should.have.property("last_commit");
payload.should.have.property("last_commit.author_name");
payload.should.have.property("last_commit.author_email");
done();
});
});
});
当我测试应用程序时,收到以下错误消息:
Uncaught AssertionError: expected Object {
"name": "myapp",
"version": "1.0.0",
"last_commit": Object {
"author_name": "Jon Snow"
"author_email": "my@email.com"
}
} to have property 'last_commit.author_name'
为什么我在这些行上收到断言错误?
payload.should.have.property("last_commit.author_name");
payload.should.have.property("last_commit.author_email");
断言正在寻找一个名为 last_commit.author_name
的 属性,但它不存在。你可能想把它分成两个断言。
payload.should.have.property("last_commit");
let last_commit = payload.last_commit;
last_commit.have.property("author_name");
我有以下 JSON
负载:
"app": {
"name": "myapp",
"version": "1.0.0",
"last_commit": {
"author_name": "Jon Snow"
"author_email": "my@email.com"
}
}
和以下 .js
文件(使用 Mocha
、Supertest
和 Should
):
var supertest = require('supertest')
var should = require('should')
var server = supertest.agent('http://localhost:3001')
describe('GET /', function () {
it('should respond with JSON', function (done) {
server
.get('/')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
var payload = res.body.app;
payload.should.have.property("app");
payload.should.have.property("name");
payload.should.have.property("version");
payload.should.have.property("last_commit");
payload.should.have.property("last_commit.author_name");
payload.should.have.property("last_commit.author_email");
done();
});
});
});
当我测试应用程序时,收到以下错误消息:
Uncaught AssertionError: expected Object {
"name": "myapp",
"version": "1.0.0",
"last_commit": Object {
"author_name": "Jon Snow"
"author_email": "my@email.com"
}
} to have property 'last_commit.author_name'
为什么我在这些行上收到断言错误?
payload.should.have.property("last_commit.author_name");
payload.should.have.property("last_commit.author_email");
断言正在寻找一个名为 last_commit.author_name
的 属性,但它不存在。你可能想把它分成两个断言。
payload.should.have.property("last_commit");
let last_commit = payload.last_commit;
last_commit.have.property("author_name");