简体   繁体   English

尝试使用Should,Mocha和Supertest读取JSON数组时出错

[英]Error when trying to read JSON array using Should, Mocha, & Supertest

I have the following JSON payload: 我有以下JSON负载:

"app": {
    "name": "myapp",
    "version": "1.0.0",
    "last_commit": {
        "author_name": "Jon Snow"
        "author_email": "my@email.com"
    }
}

and the following .js file (using Mocha , Supertest and Should ): 和以下.js文件(使用MochaSupertestShould ):

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();
            });
    });
});

When I test the app, I receive the following error message: 当我测试应用程序时,我收到以下错误消息:

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'

Why am I receiving an assertion error on the these lines? 为什么我在这些行上收到断言错误?

payload.should.have.property("last_commit.author_name");
payload.should.have.property("last_commit.author_email");

Assertion is looking for a property called last_commit.author_name which is not present. 断言正在寻找一个名为last_commit.author_name的属性,该属性不存在。 You may want to break that into two assertions. 您可能希望将其分为两个断言。

payload.should.have.property("last_commit");
let last_commit = payload.last_commit;
last_commit.have.property("author_name");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM