1

I'm writing tests to validate what an async process has written to mongo and I'm having issues with should.include to verify that a record is included. To simplify it, my projection only includes a single field (ag_status) and I receive the following error from should:

  1) Agriculture record should return at least one record with ag_status to true:
     Uncaught AssertionError: expected [ { ag_status: true },
  { ag_status: true },
  { ag_status: true },
  { ag_status: true },
  { ag_status: true },
  { ag_status: true } ] to include an object equal to { ag_status: true }

Here is the section of code that calls the should.include. I have tried chai, chai-fuzzy and chai-things, but none of them have worked (and do not give as detailed messages as should) so I am stuck right now. Any help is greatly appreciated.

    it('Agriculture record should return at least one record with ag_status to true', function(done){
            MobAgriculture.model.find({ time_id: "AYearAgo" }, {_id: 0, ag_status: 1 }, function(err, result) {
                    should.not.exist(err);
                    should.exist(result);
                    result.should.have.length(6);
                    console.log(result);
                    var level2 = { "ag_status" : true };
                    result.should.include(level2);
                    done();
                });

        });
frank
  • 501
  • 8
  • 21

1 Answers1

0

I am not very familiar with should.js but I think you should be using includeEql as you want to verify that an object equal to level2 is present in the array, not that level2 itself is in there. From their docs:

include(obj)

Assert that the given obj is present via indexOf(), so this works for strings, arrays, or custom objects implementing indexOf.


includeEql(obj)

Assert that an object equal to the given obj is present in an Array:

Mike
  • 1,837
  • 10
  • 9
  • That helps, but I am still getting the error. I added this: 'var level2 = { ag_status: true }; [{ ag_status: true}, { ag_status: true }].should.includeEql(level2); result.should.includeEql(level2);' The first _should_ passes, but the second _should_ still fails. So, it is something with how mongoose is presenting it. But I cannot see anything wrong with it visibly. Any ideas? – frank Aug 01 '13 at 05:25
  • @frankm I see you have a console.log statement in your example code, what's the output look like? If you copy the output from the log and test with that instead does it work? – Mike Aug 01 '13 at 21:59
  • the output is the top part of the original post, which starts with "1) Agriculture record...." I was trying to simplify the _should_ test case, so I changed the mongoose projection to just output '{ ag_status: true }'. So, my mongoose output array has 6 elements and each one is '{ ag_status: true }' and the variable I am searching for is '{ ag_status: true }'. In the meantime, I have reverted to sorting the mongoose results and calling 'result[x].should.include' for each _x_ element in the array, which is a fine workaround for now. – frank Aug 02 '13 at 01:58
  • No longer in docs (perhaps this should be [`containEql`](https://github.com/visionmedia/should.js/#containeqlothervalue) now?) – Wex May 12 '14 at 00:41