title: Testing a Node.js function whose return value can change depending upon the moment an asynchronous operation is permormed
body: <p>I am writing a mock for the <a href="https://github.com/mranney/node_redis" rel="nofollow">node-redis module</a> and using <a href="http://pivotal.github.com/jasmine" rel="nofollow">Jasmine</a> 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.</p>
  
  <p>My problem is: <em>if I understood node-redis correctly</em>, 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:</p>
  
  <pre><code>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);
  });
  </code></pre>
  
  <p>it will not pass if I use the real Redis module, because there will not be enough time to connect to database.</p>
  
  <p>Is there a way to wait the asynchronous request to be executed to go ahead testing the code?</p>
  
  <p>(Answers with different approaches to this problem are welcomed too.)</p>