title: Testing Promises with multiple thens using testdoublejs
body: <p>I am using <code>testdouble</code> for stubbing calls within my node.js project. This particular function is wrapping a promise and has multiple <code>then</code> calls within the function itself. </p>
  
  <pre><code>function getUser (rethink, username) {
    return new Promise((resolve, reject) =&gt; {
    let r = database.connect();
    r.then(conn =&gt; database.table(tablename).filter({username}))
     .then(data =&gt; resolve(data))
     .error(err =&gt; reject(err));
   });
  }
  </code></pre>
  
  <p>So I am wanting to determine if the <code>resolve</code> and <code>reject</code> are handled correctly based on error conditions. Assume there is some custom logic in there that I need to validate.</p>
  
  <p>For my test</p>
  
  <pre><code>import getUser from './user';
  import td from 'testdouble';
  test(t =&gt; {
    const db = td.object();
    const connect = td.function();
    td.when(connect('options')).thenResolve();
    const result = getUser(db, 'testuser');
    t.verify(result);
  }
  </code></pre>
  
  <p>The issue is that the result of connect needs to be a promise, so I use then resolve with a value which needs to be another promise that resolves or rejects.</p>
  
  <p>The line it is relating to is the result of <code>database.connect()</code> is not a promise.</p>
  
  <pre><code>TypeError: Cannot read property 'then' of undefined
  </code></pre>
  
  <p>Anyone have success with stubbing this type of call with Test Double?</p>