       @doc.on 'ready', =>
         expect(@doc.state).to.equal 'ready'
         done()
-      sendMessage c: 'notes', d: 'music', a: 'fetch', data: { data: 'cool' , v: 0}
-
-    it 'fetches ops when we have data', ->
-
-    it 'just calls the callback when we are subscribed', ->
-
-    it 'calls all callbacks when fetch is called multiple times', ->
+      @doc._onMessage {c: 'notes', d: 'music', a: 'fetch', data: { data: 'cool' , v: 0}}
+
+    it 'fetches ops when we have data'
+
+    it 'just calls the callback when we are subscribed'
+
+    it 'calls all callbacks when fetch is called multiple times'
 
   describe '#create', ->
 
     it 'calls callback', (done)->
-      @doc.create(textType, done)
-      @doc.flush()
-      sendMessage connection.sent[0]
+      @connection.sendBackTo = @doc
+      @doc.create textType, done
 
     it 'sends create message', ->
       @doc.create(textType)
       @doc.flush()
-      expect(connection.sent[0]).to.have.property('create')
+      expect(@connection.sent[0]).to.have.property('create')
 
     it 'immediately injests data locally', ->
       @doc.create(textType, 'a note on music')
       expect(@doc.snapshot).to.equal 'a note on music'
 
     it 'injests data on success', ->
+      @connection.sendBackTo = @doc
       @doc.create(textType, 'a note on music')
       @doc.flush()
-      sendMessage connection.sent[0]
       expect(@doc.snapshot).to.equal 'a note on music'
 
     it 'gets ready', (done)->
+      @connection.sendBackTo = @doc
       expect(@doc.state).to.be.null
       @doc.create(textType, 'a note on music')
       @doc.flush()
       @doc.on 'ready', =>
         expect(@doc.state).to.equal 'ready'
         done()
-      sendMessage connection.sent[0]
 
     it 'emits create after snapshot created', (done)->
       @doc.once 'create', =>

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

    it 'emits unsubscribe', (done)->
      @doc.on('unsubscribe', done)
      sendMessage 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()
      sendMessage 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()
      sendMessage c: 'notes', d: 'music', a: 'fetch', data: { data: 'cool' , v: 0, type: 'text'}
      expect(@doc.snapshot).to.equal 'cool'

    it 'gets ready', (done)->



  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 ->
      @connection.sendBackTo = @doc
      @doc.create(textType, 'note')
      @doc.flush()
      @context = @doc.createContext()

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

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

      @doc.flush()
      expect(@doc.snapshot).to.equal 'delicious note'

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

    it 'can call removeContexts', ->
      @doc.removeContexts()


  describe 'rollback', ->
