2017-05-02 124 views
-3

任何人都知道为什么我的摩卡测试“由导演查询”会抛出错误,会真正体会到一个简洁,直截了当但高效的答案。由于断言继续抛出错误断言错误0 == 1在摩卡测试中,

什么即时试图做的是学习如何使用摩卡开展可在功能测试上我的软件,从确保我的服务器可以插入和查询我的数据库

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! 

回答

0

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

+0

感谢您的支持。当我将值增加到1时,我仍然得到相同的错误结果。你能解释更多关于如何使测试通过吗? – OAOD

+0

测试结果告诉你,你有四个结果,而不是一个。 – dmfay