最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Give ChaiMocha a partial list of keys that should be included - Stack Overflow

programmeradmin4浏览0评论

It seems that if I do

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": ""
            },
            headers: {
                "Authorization": "Bearer " + newTestUser.token
            } }, function ( err, response, body ) {

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

                done()
        })
    })
})

That this will return an error since the object ing back also has the key message. How can I have this e back as passed as long as the 3 keys in the array are present, despite any more being there. I can't always predict what's going to be there in each case.


UPDATE: Here is how I'm requiring Chai and 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 )

It seems that if I do

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./watch?v=uxfRLNiSikM"
            },
            headers: {
                "Authorization": "Bearer " + newTestUser.token
            } }, function ( err, response, body ) {

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

                done()
        })
    })
})

That this will return an error since the object ing back also has the key message. How can I have this e back as passed as long as the 3 keys in the array are present, despite any more being there. I can't always predict what's going to be there in each case.


UPDATE: Here is how I'm requiring Chai and 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 )
Share Improve this question edited Apr 11, 2015 at 22:47 Noah asked Feb 25, 2015 at 10:16 NoahNoah 4,76110 gold badges42 silver badges52 bronze badges 2
  • Not sure if it works, but what if you add the all property like body.should.include.all.keys( [ "user", "title", "content" ] ) – David Losert Commented Feb 25, 2015 at 12:35
  • 1 One thing to note is that chai.should() modifies Object.prototype by adding its own function as Object.prototype.should. While violates accepted 'best practice', it is usually accepted since the value of extending the Object prototype in this way outweighs any possible issues. The only instance where this can bite you is if you have an object that also defined its own prototypal should(), in which case, testing will fail. Just a word to the wise. ;) – Rob Raisch Commented Apr 14, 2015 at 21:38
Add a ment  | 

2 Answers 2

Reset to default 6 +50

If you have an object like this one (similar to what you described):

var obj= {
  user: "user",
  title: "title",
  content: "content",
  message: "message"
};

all the following assertions should pass:

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"]);

even if you pass the values as separated arguments:

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");

So, assuming that you required chai's should style correctly:

var should = require('chai').should();

, your problem may be just a typo in your body object or in your test suite.

UPDATE: After you added more information about the way you required all testing modules, a couple things should be pointed out:

  • First, you required chai two times, the second one when setting up should. You did:

    var chai = require( 'chai' ),
        should = require( 'chai' ).should(),
        ...
    

    when you should've done:

    var chai = require( 'chai' ),
        should = chai.should(),
        ...
    
  • Second, if you're using chai-as-promised, you should assert body keys the way this module requires, like:

    Promise.resolve(body).should.eventually.include.keys([ "user", "title", "content" ]);
    

Wouldn't the simplest solution be, within your request handler, to:

function ( err, response, body ) {
  var expected={},
      expected_keys=['user','title','content'];

  expected_keys.forEach(function(key){
    expected[key]=body[key];
  });

  expected.should.include.keys(expected_keys);

  done();
}

Or using one of my fav tools, lodash:

var _=require('lodash');

function ( err, response, body ) {
  var expected_keys=['user','title','content'];

  _.pick(body,expected_keys).should.include.keys(expected_keys);

  done();
}
发布评论

评论列表(0)

  1. 暂无评论