2017-05-02 161 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