Frage

I want to use mocha to add to my program feature by feature, test by test.

var assert = require('assert');
var mongoskin = require('mongoskin');

describe('basic database tests', function(){
  before(function(){
  });
  it('should have 3 users', function(done){
    var db = mongoskin.db('mongodb://localhost:27017/stuffTest', {safe:true});
    db.collection('users').find().toArray(function  (err,result){
      console.log(result.length);
      assert.equal(result.length,3);
    });
  });
});

It doesn't work. I get an error no matter where I put stuff in the test. With this arrangement I get Error: timeout of 2000ms exceeded

This is the code to set up the database. My old way of development was to litter my code with console.logs and such. This code uses console.logs to let me know if the collection is empty and then if so was it filled with 3 records.

var mongoskin = require('mongoskin')
var db = mongoskin.db('mongodb://localhost:27017/stuffTest', {safe:true})
db.collection('users').find().toArray(function  (err,result){
  console.log(result.length)
})

db.collection('users', {strict:true}, function(err, collection) {
    if (err) {
        console.log("The 'users' collection doesn't exist. Creating it with sample data...");
        populateDB(users);
    }
});

var populateDB = function(huh) {
    console.log("Populating database...");
    var name= huh.name;
    var coll= huh.items;
    db.collection(name, function(err, collection) {
        collection.insert(coll, {safe:true}, function(err, result) {
          console.log(result.length);
        });
    });
}; 

var users = [];
users.name = 'users';
users.items= [
{name: 'tim', email: 'mckenna.tim@gmail.com', lists:[]},
{name: 'peri', email: 'perimckenna@gmail.com', lists:[]},
{name: 'tim2', email: 'mckt_jp@yahoo.com', lists:[]}
];

How would I write this test? This code plus package.json and dropDb.js is here: https://github.com/mckennatim/tdd

War es hilfreich?

Lösung

You're not calling done. If you don't call done in an asynchronous test you are guaranteed to get a timeout. Modify the test to call done at the end of your callback. Like this:

  it('should have 3 users', function(done){
    var db = mongoskin.db('mongodb://localhost:27017/stuffTest', {safe:true});
    db.collection('users').find().toArray(function  (err,result){
      console.log(result.length);
      assert.equal(result.length,3);
      done();
    });
  });
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top