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 comparison 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();
});
});
});
The assert module provides a way of testing expressions. If the expression evaluates to 0, or false, an assertion failure is being caused, and the program is terminated. This module was built to be used internally by Node. js.
Assertions are errors that may occur on a MongoDB server from time to time. Administrators need to promptly capture these errors and understand their nature, so that they can rapidly resolve them and prevent any negative impact on database performance.
The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.
Assert is used to perform a simple test to compare 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.org/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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With