断言在mocha测试期间不断抛出AssertionError 0 == 1,

时间:2017-05-02 21:24:59

标签: node.js testing mocha

任何人都知道为什么我的摩卡测试由导演查询'抛出错误,真的会欣赏一个简洁,直接但有效的答案。谢谢

我想要做的是学习如何使用mocha对我的软件上可用的功能进行测试,从确保我的服务器可以插入和查询我的数据库开始

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://mongodb.github.io/node-mongodb-native/2.0/api/Cursor.html#sort
         */
        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();
        });
      });

//使用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!

1 个答案:

答案 0 :(得分:0)

看看assert.equal(docs.length, 0);。鉴于下一行断言第一个结果具有这样的标题,我猜测你的查询返回零结果的断言是不正确的。