I am writing a mock for the node-redis module and using Jasmine to test it. I write tests for various aspects of Redis commands and my intention is to be able to run the tests against the original Redis module as well.
My problem is: if I understood node-redis correctly, the returned value of the asynchronous functions of node-redis is different depending upon the command was sent to Redis or queued for sending later (for example, to be sent after the connection is complete). But I would like to test the returned value, too, and if I write a test such as the one below:
it("should update value", function () {
var client = redis.createClient();
client.set("key", "1st");
var value = client.get("key", function (err, data) {
expect(err).toBeNull();
expect(data).toBe("1st");
});
expect(value).toBe(true);
});
it will not pass if I use the real Redis module, because there will not be enough time to connect to database.
Is there a way to wait the asynchronous request to be executed to go ahead testing the code?
(Answers with different approaches to this problem are welcomed too.)
expect(err).toBeNull() == expect(value).toBe(true);because asynchronous functions return nothing (undefined). You should do this onlyBut if you want to wait you should use something like this: