admin管理员组文章数量:1302272
This is an example from mongoDB driver documentation. I've been trying to figure out what assert.equal does in the example, but the official documentation on Node.js website didn't help me too much -- The official documentation says "Tests shallow, coercive equality with the equal parison operator ( == )." So I suspected first that it would return true or false depending on the truth value of the equality. But it seems like it doesn't.
I did look at this post: Assert module use in nodejs?. It did help -- but wasn't quite enough. I still don't really understand how "unit test" is done. Any help would be appreciated but solid examples would be super helpful!
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();
});
});
});
This is an example from mongoDB driver documentation. I've been trying to figure out what assert.equal does in the example, but the official documentation on Node.js website didn't help me too much -- The official documentation says "Tests shallow, coercive equality with the equal parison operator ( == )." So I suspected first that it would return true or false depending on the truth value of the equality. But it seems like it doesn't.
I did look at this post: Assert module use in nodejs?. It did help -- but wasn't quite enough. I still don't really understand how "unit test" is done. Any help would be appreciated but solid examples would be super helpful!
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();
});
});
});
Share
Improve this question
edited May 23, 2017 at 12:24
CommunityBot
11 silver badge
asked Jan 24, 2015 at 19:09
ChannyChanny
892 silver badges8 bronze badges
1
- oh I think I might have figured it out... the program stops execution and throws an error message when the assertion is not true? – Channy Commented Jan 24, 2015 at 19:19
1 Answer
Reset to default 7Assert is used to perform a simple test to pare an expected result with an actual result. If the actual result does not match the expected result an exception will be thrown. This feature is meant for development purposes only.
It will stop the execution. I ran a simple node js server that contained an assert (assert.equal(3, 1);
) and it stopped the execution because 3 does not equal 1.
The following assert will throw an exception if the parameter err
is not null:
assert.equal(null, err);
See the following link for more information: https://nodejs/api/assert.html
Here is a description of assert from the link above:
The assert module provides a simple set of assertion tests that can be used to test invariants. The module is intended for internal use by Node.js, but can be used in application code via require('assert'). However, assert is not a testing framework, and is not intended to be used as a general purpose assertion library.
本文标签: javascriptnodejs assert module in mongodb driver documentationStack Overflow
版权声明:本文标题:javascript - node.js assert module in mongodb driver documentation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741694807a2392943.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论