tj / should.js

BDD style assertions for node.js -- test framework agnostic
MIT License
2.75k stars 194 forks source link

Should Include Object #101

Closed rhythmicdevil closed 11 years ago

rhythmicdevil commented 11 years ago

Hi, I am having some problems with the correct way to assert the value of an object. The method (increment) that I am testing returns an object like this:

{ 
    selector: { 
        field: 'rpr' 
    },
   type: 'increment',
   update: { 
        field: 'count', 
        value: 1 
   }
}

My test method looks like this:

describe('Increment', function() {

        it('Should return query, config, and options objects', function(done){

            var feature = { 
                key: 'rpr',
                type: 'number',
                criteria: [{ 
                    type: 'greater_than', 
                    val: 0 
                },
                { 
                    type: 'less_than', 
                    val: 3 
                }],
                actions: [{ 
                    type: 'increment', 
                    dbconfig: { 
                        collection: 'counters'
                    }
                }],
                regex: /rpr:(([^ ])+)/,
                extracted: '1' 
            };

            var action = {
                type: 'increment', 
                dbconfig: { 
                    collection: 'counters' 
                }
            };

            increment(feature, action, function(query, config, options){

console.log(query);
console.log(config);

                var q_obj = 
                    { selector: { 
                        field: 'rpr' 
                    },
                    type: 'increment',
                    update: { 
                        field: 'count', 
                        value: 1 
                    } 
                };

                query.should.be.a('object').and.should.include(q_obj);
                config.should.be.a('object');
                options.should.be.a('object');

                //query.should.have.property('selector').and.should.include({field: 'rpr' });
                query.should.have.property('type', 'increment');
                query.should.have.property('update');

                config.should.have.property('collection', 'counters');
                done();

            });

        });

});

This always fails with the following message:

 { selector: { field: 'rpr' },
  type: 'increment',
  update: { field: 'count', value: 1 } }
{ collection: 'counters' }
․

  ✖ 1 of 1 test failed:

  1) Increment Should return query, config, and options objects:
     AssertionError: expected { obj: 
   { selector: { field: 'rpr' },
     type: 'increment',
     update: { field: 'count', value: 1 } } } to include an object equal to { selector: { field: 'rpr' },
  type: 'increment',
  update: { field: 'count', value: 1 } }

If I leave off the line:

.and.should.include(q_obj);

it passes. It looks to me like the object should pass the assertion.

What I want to do is assert that the returned object for 'query' is of that form described in 'q_obj'. Can you point me the right direction?

Thanks Steve

btd commented 11 years ago

Don't know if it is actual or not, but include expect work (but imho i would expect as you) on exact object to compare by keys and values:

query.obj.should.include(q_obj); //this works

I will close it for now. Open new if you have questions/problems about include or other things in should.js.