         @doc.flush()
         expect(@context.get()).to.equal 3
 
-        msg = connection.sent.pop()
+        msg = @connection.sent.pop()
         msg.error = 'rejected'
         msg.a = 'ack'
-        sendMessage msg
+        @doc._onMessage msg
         expect(@context.get()).to.equal 1
 
 
     it 'ends up in the right state if we create() then subscribe() synchronously'
-    it "abandons the document state if we can't recover from the rejected op", ->
+    it "abandons the document state if we can't recover from the rejected op"
 
   describe 'after op event', ->
 

        @doc.create textType, 'a note on music'
        @doc.flush()
        expect(@doc.state).to.equal 'floating'
        sendMessage c: 'notes', d: 'music', a: 'ack', error: 'rejected'
        expect(@doc.state).to.be.null

      it "when we're ready and the server rejects the op", ->

      it "when the doc is floating and the document already exists on the server", ->

    describe 'operation', ->


      @doc.unsubscribe =>
        expect(@doc.subscribed).to.be.false
        done()
      @doc._onMessage {c: 'notes', d: 'music', a: 'unsub'}

    it 'emits unsubscribe', (done)->
      @doc.on('unsubscribe', done)
      @doc._onMessage {c: 'notes', d: 'music', a: 'unsub'}

    it 'sends unsubscription message', ->
      @doc.unsubscribe()
      expect(@connection.sent[0]).to.deep.equal {c: 'notes', d: 'music', a: 'unsub'}

    it 'calls subscribe callbacks', (done)->
      @doc.subscribe(done)
      @doc.unsubscribe()
      @doc._onMessage {c: 'notes', d: 'music', a: 'unsub'}


  describe '#fetch', ->

    it 'sends fetch message', ->
      @doc.fetch()
      expect(@connection.sent[0]).to.deep.equal {c: 'notes', d: 'music', a: 'fetch'}

    it 'sets snapshot', ->
      @doc.fetch()
      @doc._onMessage {c: 'notes', d: 'music', a: 'fetch', data: { data: 'cool' , v: 0, type: 'text'}}
      expect(@doc.snapshot).to.equal 'cool'

    it 'gets ready', (done)->
