mardi 2 mai 2017

assertion keeps throwing error AssertionError 0 == 1 during mocha test,

Anyone know why my mocha tests for 'querying by director' throws errors, will really appreciate a concise,straight-to-the-point but efficient answer. Thanks

What im trying to do is learn how to use mocha to carry out test on functionality available on my software, starting from ensuring my server can insert and query my database

interface.js //code block carrying the insert and find commands


         /*
             *  Inserts "doc" into the collection "movies".
             */
                exports.insert = function(db, doc, callback) {
          // TODO: implement
          db.collection('movies').insert(doc);
          callback(null);
        };

        /*
         *  Finds all documents in the "movies" collection
         *  whose "director" field equals the given director,
         *  ordered by the movie's "title" field. See
         *  http://ift.tt/1PPOdRG
         */
        exports.byDirector = function(db, director, callback) {
          // TODO: implement
          db.collection('movies').find({director: director});
          callback(null, []);
        };

    test.js //code block for running tests

    var assert = require('assert');
    var connect = require('./connect');
    var dbInterface = require('./interface');
    var fs = require('fs');
    var movies = require('./movies');

    /**
     *  This test suite is meant to be run through gulp (use the `npm run watch`)
     *  script. It will provide you useful feedback while filling out the API in
     *  `interface.js`. You should **not** modify any of the below code.
     */
    describe('dbInterface', function() {
      var db;
      var succeeded = 0;
      var georgeLucasMovies;

      /**
       *  This test ensures that interface.js' `insert()` function properly inserts
       *  a document into the "movies" collection.
       */
      it('can insert a movie', function(done) {
        var doc = { title: 'Rogue One', year: 2016, director: 'Gareth Edwards' };
        dbInterface.insert(db, doc, function(error) {
          assert.ifError(error);
          db.collection('movies').count({ title: 'Rogue One' }, function(error, c) {
            assert.ifError(error);
            assert.equal(c, 1);
            done();
          });
        });
      });

      /**
       *  This test ensures that interface.js' `byDirector()` function can load a
       *  single document.
       */
      it('can query data by director', function(done) {
        dbInterface.byDirector(db, 'Irvin Kershner', function(error, docs) {
          assert.ifError(error);
          assert.ok(Array.isArray(docs));
          assert.equal(docs.length, 0);
          assert.equal(docs[0].title, 'The Empire Strikes Back');
          ++succeeded;
          done();
        });
      });

//response from running test with npm

            [21:58:30] Starting 'test'...
            [21:58:30] Finished 'test' after 2.04 ms


            dbInterface
            ✓ can insert a movie
            1) can query data by director
            2) returns multiple results ordered by title


            1 passing (246ms)
            2 failing


            1) dbInterface can query data by director:
               TypeError: Cannot read property 'title' of undefined
               at test.js:42:27
               at Object.exports.byDirector (interface.js:19:3)
               at Context.<anonymous> (test.js:38:17)

            2) dbInterface returns multiple results ordered by title:

              AssertionError: 0 == 4
              + expected - actual

              +4
              -0

              at test.js:57:14
              at Object.exports.byDirector (interface.js:19:3)
              at Context.<anonymous> (test.js:54:17)



              Tests failed!

Aucun commentaire:

Enregistrer un commentaire