title: How to stub a class which returns a class?
body: <p>I have a method which i'm unit testing and i'm trying to test both </p>
  
  <pre><code>    ...
      let db = new MockDb()
      ...
      db.query(query)
          .then(cursor =&gt; {
            return cursor.next()
              .then(result =&gt; {
                if (result !== undefined || !data.id) { &lt;-- WANT TO STUB CURSOR.NEXT() SO I CAN TEST RESULT BEING DIFFERENT VALUES
                  data.id = uuidv4()
                }
      ...
  </code></pre>
  
  <p>I have this file that is used for mocking/stubbing which contains 2 classes, but only exposes one of them</p>
  
  <pre><code>class MockDb {  
    query (aql) {
      return new Promise((resolve, reject) =&gt; {
        return resolve(new MockCursor())
      })
    }
  }
  
  
  class MockCursor {
    next () {
      return new Promise((resolve, reject) =&gt; {
        return resolve({foo: 'bar'})
      })
    }
  }
  
  module.exports = MockDb
  </code></pre>
  
  <p>How can I stub what <code>cursor.next()</code> returns in this case?</p>
  
  <p>Does NOT work - gives error <code>TypeError: cursor.stub is not a function</code></p>
  
  <pre><code>before(() =&gt; {
    let cursor = sinon.stub()
    cursor.stub('next').return(undefined)
    sinon.stub(db, 'query').resolves(cursor)
  })
  </code></pre>
  
  <p>If i expose the <code>MockCursor</code> class I can simply do this:</p>
  
  <pre><code>let db = new MockDb()
  let cursor = new MockCursor()
  sinon.stub(cursor, 'next').resolves(undefined)
  sinon.stub(db, 'query').resolves(cursor)
  </code></pre>
  
  <p>But i was just trying to do it without having to expose the MockCursor class explicitly</p>