node.js在mongodb驱动程序文档中声明模块

这是mongoDB驱动程序文档的一个例子。 我一直在试图找出assert.equal在这个例子中做了些什么,但是在Node.js网站上的官方文档并没有太多的帮助 – 官方文档中写着:“testing浅,与平等比较运算符强制平等(==)“。 所以我首先怀疑它会根据平等的真值返回真假。 但似乎没有。

我看了这个post: 断言模块在nodejs中使用? 。 它确实有帮助 – 但还不够。 我还是不太了解“unit testing”是如何完成的。 任何帮助将不胜感激,但坚实的例子将是超级有益的!

var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); // Set up the connection to the local db var mongoclient = new MongoClient(new Server("localhost", 27017), {native_parser: true}); // Open the connection to the server mongoclient.open(function(err, mongoclient) { // Get the first db and do an update document on it var db = mongoclient.db("integration_tests"); db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); // Get another db and do an update document on it var db2 = mongoclient.db("integration_tests2"); db2.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); // Close the connection mongoclient.close(); }); }); }); 

断言用于执行简单的testing,以将预期结果与实际结果进行比较。 如果实际结果与预期结果不符,则会抛出exception。 此function仅用于开发目的。

它会停止执行。 我运行了一个包含assert( assert.equal(3, 1); )的简单节点js服务器,并停止执行,因为3不等于1。

如果参数err不为null,那么下面的assert将会抛出一个exception:

 assert.equal(null, err); 

有关更多信息,请参阅以下链接: https : //nodejs.org/api/assert.html

这里是从上面的链接断言的描述:

断言模块提供了一组简单的断言testing,可以用来testing不variables。 该模块旨在供Node.js内部使用,但可以通过require('assert')在应用程序代码中使用。 但是,assert不是一个testing框架,不能用作通用的断言库。