On moving to TDD with node and mongo

mcktimo

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: '[email protected]', lists:[]},
{name: 'peri', email: '[email protected]', lists:[]},
{name: 'tim2', email: '[email protected]', lists:[]}
];

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

Louis

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();
    });
  });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在使用node和mongo迁移到TDD时

来自分类Dev

Node Mongo - 查找多个参数

来自分类Dev

node + mongo: updating a record requires a callback

来自分类Dev

将Node / Mongo部署到Openshift

来自分类Dev

node + mongo:更新记录需要回调

来自分类Dev

Node.js,Mongo查找并返回数据

来自分类Dev

Mongo:使用Node仅查询今天的文档

来自分类Dev

Mongo Node JS查询隐式$ and

来自分类Dev

在Node / Express中嵌套Mongo调用

来自分类Dev

Node中的Mongo中是否有更好的处理并发的方法?

来自分类Dev

如何从Node中的Mongo(带有Mongoose)访问`findAndModify`方法?

来自分类Dev

Node.js,Mongo async.js插入和查询

来自分类Dev

Mongo DB和Node.js连接错误

来自分类Dev

Mac:Node JS Mongo数据库错误:连接被拒绝

来自分类Dev

如何隐藏mongo终端(Node.Js)的输出

来自分类Dev

Node JS如何将价值保存到Mongo DB

来自分类Dev

无法从Mongo / Node中的cursor.toArray()获得响应

来自分类Dev

Mongo DB和Node.js连接错误

来自分类Dev

Mongo无法与Node.js一起使用

来自分类Dev

使用Node.js进行回调递归插入Mongo

来自分类Dev

Mac:Node JS Mongo数据库错误:连接被拒绝

来自分类Dev

变量替换在node js的mongo中在$ in中不起作用

来自分类Dev

Node、Mongo、Mongoos - 发送后无法设置标头

来自分类Dev

Node应用中如何使用`mongo DB`数据进行分析

来自分类Dev

TDD Browserify和ReactJS

来自分类Dev

TDD:测试等待功能

来自分类Dev

TDD中的模拟值

来自分类Dev

BDD如何补充TDD

来自分类Dev

Bob叔叔的TDD规则