           fake_results.query_counter = parseInt query_counter
           fake_results
         )
-
-    beforeEach () ->
       @$ = doofinder.jQuery
       # reset the query input, to get rid of callbacks
       @$('body').empty().append('<input type="text" id="query"></input><div id="results"></div>')
-
-
-    it 'display search results', (done) ->
+      # set up doofinder controller
       client = new doofinder.Client mock.request.hashid, mock.request.api_key,5,null,'fooserver'
       queryInputWidget = new doofinder.widgets.QueryInput '#query'
       resultsWidget = new doofinder.widgets.Results '#results'
       controller = new doofinder.Controller client
       controller.addWidget queryInputWidget
       controller.addWidget resultsWidget
-      resultsContainer = @$ '#results'
-      queryEl = @$ '#query'
+      # the els in questionr
+      @resultsContainer = @$ '#results'
+      @queryEl = @$ '#query'
+
+    it 'display search results', (done) ->
       # when results are received, we check they're the "fake results"
+      resultsContainer = @resultsContainer
       resultsContainer.one 'DOMSubtreeModified', () ->
         resultsContainer.find('li').length.should.be.equal 2
         resultsContainer.find('li b')[0].innerHTML.should.contain "Aironet"
         done()
-      @$('#query').val 'xill'
+      @queryEl.val 'xill'
       # search!
-      @$('#query').trigger 'keydown'
+      @queryEl.trigger 'keydown'
 
     it 'replaces search results', (done) ->
-      client = new doofinder.Client mock.request.hashid, mock.request.api_key,5,null,'fooserver'
-      queryInputWidget = new doofinder.widgets.QueryInput '#query'
-      resultsWidget = new doofinder.widgets.Results '#results'
-      controller = new doofinder.Controller client
-      controller.addWidget queryInputWidget
-      controller.addWidget resultsWidget
-      resultsContainer = @$ '#results'
-      queryEl = @$ '#query'
-
+      resultsContainer = @resultsContainer
+      queryEl = @queryEl
       resultsContainer.one 'DOMSubtreeModified', () ->
         # we just inserted fake_div
         resultsContainer.find('#fake_div').length.should.be.equal 1

    navigator.userAgent = 'Nasty Navigator' # kudos to @jesusenlanet: great Name!
    navigator.appVersion = '0.0.1'
    global.doofinder = require "../lib/doofinder.js"
    @client = new doofinder.Client mock.request.hashid, mock.request.api_key

  afterEach () ->
    delete global.document


  context 'the results widget', ->

    beforeEach () ->
      scope = nock('http://fooserver')
        .get('/5/search')
        .query(true)
        .reply((uri, requestBody)->
