         @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', ->
 



  describe 'subscribe unsubscribe and fetch', ->
    it 'subscribes once and calls all callbacks when subscribe is called multiple times', ->
    it 'unsubscribes once and calls all callbacks when unsubscribe is called multiple times', ->
    it 'hydrates the document if you call getOrCreate() with no data followed by getOrCreate() with data'


  describe 'editing contexts', ->

    beforeEach ->
      @doc.create(textType, 'note')
      @doc.flush()
      sendMessage connection.sent.pop()
      @context = @doc.createContext()

    afterEach ->
      @doc.removeContexts()


    it '#get exposes data', ->
      expect(@context.get()).to.equal 'note'

    it 'changes snapshot locally', ->
      @context.insert(0, 'delicious ')
      @doc.flush()
      expect(@doc.snapshot).to.equal 'delicious note'

      sendMessage connection.sent.pop()
      expect(@doc.snapshot).to.equal 'delicious note'

    it 'changes context data', ->
      @context.insert(0, 'delicious ')
      @doc.flush()
      sendMessage connection.sent.pop()
      expect(@context.get()).to.equal 'delicious note'


  describe 'rollback', ->

    it 'sends del message', ->
      @doc.del()
      @doc.flush()
      expect(@connection.sent[0]).to.have.property('del')

    it 'unsets type', ->
      @doc.del()
