给 Chai/Mocha 应该包含的部分键列表

Noa*_*oah 3 javascript unit-testing mocha.js node.js chai

看来,如果我这样做

describe( 'Add Youtube', function () {

    it( 'should return the video data, including user, title and content fields', function ( done ) {

        this.timeout( 5000 )

        request({
            method: 'POST',
            url: 'https://localhost:8443/api/add',
            json: true,
            strictSSL: false,
            body: {
                "type": "youtube",
                "url": "https://www.youtube.com/watch?v=uxfRLNiSikM"
            },
            headers: {
                "Authorization": "Bearer " + newTestUser.token
            } }, function ( err, response, body ) {

                body.should.include.keys( [ "user", "title", "content" ] )

                done()
        })
    })
})
Run Code Online (Sandbox Code Playgroud)

这将返回一个错误,因为返回的对象也有 key message。只要数组中的 3 个键存在,我怎么能让它回来,尽管还有更多。我不能总是预测每种情况下会发生什么。


更新:这是我如何要求 Chai 和should.

var chai = require( 'chai' ),
    chaiAsPromised = require( 'chai-as-promised' ),
    should = require( 'chai' ).should(),
    path = require( 'path' ),
    getUser = require( '../helpers/get-user' ),
    userController = require( '../controllers/userController' ),
    blogController = require( '../controllers/blogController' ),
    request = require( 'request' ),
    User = require( '../models/userModel' ),
    Content = require( '../models/contentModel' ),
    shortid = require( 'shortid' )

chai.use( chaiAsPromised )
Run Code Online (Sandbox Code Playgroud)

Rod*_*ros 5

如果您有这样的对象(类似于您所描述的):

var obj= {
  user: "user",
  title: "title",
  content: "content",
  message: "message"
};
Run Code Online (Sandbox Code Playgroud)

以下所有断言都应该通过:

obj.should.include.keys(["user", "title", "content"]);
obj.should.includes.keys(["user", "title", "content"]);
obj.should.contain.keys(["user", "title", "content"]);
obj.should.includes.keys(["user", "title", "content"]);
Run Code Online (Sandbox Code Playgroud)

即使您将值作为分隔参数传递:

obj.should.include.keys("user", "title", "content");
obj.should.includes.keys("user", "title", "content");
obj.should.contain.keys("user", "title", "content");
obj.should.includes.keys("user", "title", "content");
Run Code Online (Sandbox Code Playgroud)

因此,假设您正确使用了requiredchaishould样式:

var should = require('chai').should();
Run Code Online (Sandbox Code Playgroud)

,您的问题可能只是您的body对象或测试套件中的拼写错误。

更新:在您添加了有关您需要所有测试模块的方式的更多信息后,应指出以下几点: