Javascript mongodb驱动程序文档中的node.js断言模块
这是mongoDB驱动程序文档中的一个示例。我一直在试图弄清楚assert.equal在示例中的作用,但是Node.js网站上的官方文档对我帮助不大——官方文档说“使用相等比较运算符(=)测试肤浅的强制相等。”所以我首先怀疑它会根据等式的真值返回true或false。但似乎没有 我确实看过这个帖子:。这确实有帮助,但还不够。我仍然不太明白“单元测试”是如何完成的。任何帮助都将不胜感激,但坚实的例子将非常有用Javascript mongodb驱动程序文档中的node.js断言模块,javascript,node.js,mongodb,module,assert,Javascript,Node.js,Mongodb,Module,Assert,这是mongoDB驱动程序文档中的一个示例。我一直在试图弄清楚assert.equal在示例中的作用,但是Node.js网站上的官方文档对我帮助不大——官方文档说“使用相等比较运算符(=)测试肤浅的强制相等。”所以我首先怀疑它会根据等式的真值返回true或false。但似乎没有 我确实看过这个帖子:。这确实有帮助,但还不够。我仍然不太明白“单元测试”是如何完成的。任何帮助都将不胜感激,但坚实的例子将非常有用 var Db = require('mongodb').Db, MongoCli
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();
});
});
});
Assert用于执行一个简单的测试,以将预期结果与实际结果进行比较。如果实际结果与预期结果不匹配,将引发异常。此功能仅用于开发目的 它将停止执行。我运行了一个包含assert(
assert.equal(3,1);
)的简单节点js服务器,它停止了执行,因为3不等于1
如果参数err
不为null,则以下断言将引发异常:
assert.equal(null, err);
有关详细信息,请参阅以下链接:
下面是上面链接中的assert说明:
assert模块提供了一组简单的断言测试,可以
用于测试不变量。该模块供以下人员内部使用:
Node.js,但可以通过require('assert')在应用程序代码中使用。
然而,assert不是一个测试框架,也不打算成为一个测试框架
用作通用断言库
哦,我想我可能已经明白了。。。当断言不正确时,程序停止执行并抛出错误消息?如果
assert.equal()
仅用于开发环境,那么在生产环境中应该使用什么,可能是这样:collection.findOne({“page\u title”:page\u title},function(err,doc){If(err){res send(err)}})
?