This function is asynchronous and (because it's JavaScript) there's no way to make it synchronous. Hence, it's impossible for test(2000, 1)
to return the cursor.
What you need to do instead is pass a callback and then test the result against the value provided by the callback. Keep in mind that in Node.js, by convention, we pass an error and a result to a callback function.
Hence, you would need to write your test like this:
function test(room_number, user_id, callback) {
return r.table('wins').filter({"room_number": room_number, "user_id" : user_id}).count().run(connection, callback);
}
If you want the results as an array, you would have to to the following:
function test(room_number, user_id, callback) {
return r.table('wins').filter({"room_number": room_number, "user_id" : user_id}).count().run(connection, function(err, cursor) {
if (err) callback(err, null);
cursor.toArray(callback);
};
}
Now, when you run your test, you have to pass a callback with will compare what you expect the result to be, with the actual result.
test(2000, 1, function (err, cursor) { // If you use the first cursor
// Convert cursor to array
cursor.toArray(function (err, result) {
// Compare `result` with expectations
// Make sure that `result` is equal to [1, 2, 3];
assert.deepEqual(result, [1, 2, 3]);
});
})
Just to be clear, this is a JavaScript problem and has nothing to do specifically with how RethinkDB works. Because RethinkDB is executing a network call, that operation must be asynchronous.
Using Mocha
If you use Mocha as your testing framework, you can do the following.
it('should equal [1, 2, 3]', function (done) {
return r.table('wins').filter({"room_number": room_number, "user_id" : user_id}).count().run(connection, function(err, cursor) {
if (err) callback(err, null);
cursor.toArray(function () {
// Compare `result` with expectations
// Make sure that `result` is equal to [1, 2, 3];
assert.deepEqual(result, [1, 2, 3]);
// Tell Mocha that you're done testing and
// want to continue to the next test
done();
});
};