Commits

Dan Dirks committed dc7b6aa

Switches to directly mocking World in acceptance test.

Rather than doing hacky require cache insertion, we can just directly
mock the World in acceptance's unit tests. This should be more easily
understood, hopefully.

Comments (0)

Files changed (2)

test/acceptance.test.coffee

 assert = require('assert')
 util = require('util')
 sinon = require('sinon')
-world = require('./mocks/zombie-world-mock')
+#replace the default zombie used in files with a mocked version
+require('./mocks/zombie-mock')
 Acceptance = require('../lib/acceptance')
 
 
 suite 'acceptance.coffee', ->
   setup (done) ->
-    callback = ->
-      process.nextTick(done)
+    callback = =>
+      process.nextTick =>
+        @mockWorld = sinon.mock(@acceptance.World)
+        done()
     @acceptance = new Acceptance(callback)
 
   teardown ->
-    world.mock.verify()
+    @mockWorld.verify()
     Acceptance::pages = {}
 
 
         'NotMyHome': '/jerkshome'
 
       callback = sinon.spy()
-      world.mock.expects('visit').once().withExactArgs('/', callback)
+      @mockWorld.expects('visit').once().withExactArgs('/', callback)
 
       @acceptance.visit('Home', callback)
 
         'Not(My)?Home': '/yourhome'
 
       callback = sinon.spy()
-      world.mock.expects('visit').once().withExactArgs('/myhome', callback)
+      @mockWorld.expects('visit').once().withExactArgs('/myhome', callback)
 
       @acceptance.visit('Home', callback)
 
       field = 'jeans'
       element = Object.create(null)
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']").returns(element)
-      world.mock.expects('type').once().withExactArgs(element, text, callback)
+      @mockWorld.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']").returns(element)
+      @mockWorld.expects('type').once().withExactArgs(element, text, callback)
 
       @acceptance.enterTextInField(text, field, callback)
 
       button = 'pants'
       element = Object.create(null)
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs("button:contains('#{button}')").returns(element)
-      world.mock.expects('click').once().withExactArgs(element, callback)
+      @mockWorld.expects('findElement').once().withExactArgs("button:contains('#{button}')").returns(element)
+      @mockWorld.expects('click').once().withExactArgs(element, callback)
 
       @acceptance.clickButton(button, callback)
 
       checkbox = 'pants'
       element = Object.create(null)
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
-      world.mock.expects('check').once().withExactArgs(element, callback)
+      @mockWorld.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
+      @mockWorld.expects('check').once().withExactArgs(element, callback)
 
       @acceptance.toggleCheckboxState(state, checkbox, callback)
 
       checkbox = 'pants'
       element = Object.create(null)
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
-      world.mock.expects('uncheck').once().withExactArgs(element, callback)
+      @mockWorld.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
+      @mockWorld.expects('uncheck').once().withExactArgs(element, callback)
 
       @acceptance.toggleCheckboxState(state, checkbox, callback)
 
         '(My)?Home': '/myhome'
         'Not(My)?Home': '/yourhome'
       callback = sinon.spy()
-      world.mock.expects('path').once().returns('/myhome')
+      @mockWorld.expects('path').once().returns('/myhome')
 
       @acceptance.verifyCurrentPage('MyHome', callback)
       assert(callback.called)
         '(My)?Home': '/myhome'
         'Not(My)?Home': '/yourhome'
       callback = sinon.spy()
-      world.mock.expects('path').once().returns('/yourhome')
+      @mockWorld.expects('path').once().returns('/yourhome')
 
       assert.throws (=>
         @acceptance.verifyCurrentPage('MyHome', callback)),
       Acceptance::pages =
         'Not(My)?Home': '/yourhome'
       callback = sinon.spy()
-      world.mock.expects('path').once().returns('/yourhome')
+      @mockWorld.expects('path').once().returns('/yourhome')
 
       assert.throws (=>
         @acceptance.verifyCurrentPage('MyHome', callback)),
       """
       element = Object.create(null, { textContent: { value: bodyText } })
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs('body').returns(element)
+      @mockWorld.expects('findElement').once().withExactArgs('body').returns(element)
 
       @acceptance.verifyTextOnPage("everything!\nERMAHGERD!", callback)
       assert(callback.called)
       """
       element = Object.create(null, { textContent: { value: bodyText } })
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs('body').returns(element)
+      @mockWorld.expects('findElement').once().withExactArgs('body').returns(element)
 
       assert.throws (=>
         @acceptance.verifyTextOnPage("ERMAHGERD!!!!!!1!", callback)),
     test 'should not fail if field does not exist on the page', ->
       field = 'pants'
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']").throws()
+      @mockWorld.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']").throws()
 
       @acceptance.verifyFieldDoesntExist(field, callback)
       assert(callback.called)
         assert(not callback.called)
         done()
 
-      world.mock.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']")
+      @mockWorld.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']")
 
       @acceptance.verifyFieldDoesntExist(field, callback)
 
       field = 'pants'
       element = Object.create(null, { value: { value: text } })
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']").returns(element)
+      @mockWorld.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']").returns(element)
 
       @acceptance.verifyTextInField(text, field, callback)
       assert(callback.called)
       field = 'pants'
       element = Object.create(null, { value: { value: 'nope' } })
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']").returns(element)
+      @mockWorld.expects('findElement').once().withExactArgs("input[name='#{field}'], textarea[name='#{field}']").returns(element)
 
       assert.throws (=>
         @acceptance.verifyTextInField(text, field, callback)),
       state = 'checked'
       element = Object.create(null, { checked: { value: true } })
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
+      @mockWorld.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
 
       @acceptance.verifyCheckboxState(checkbox, state, callback)
       assert(callback.called)
       state = 'unchecked'
       element = Object.create(null, { checked: { value: false } })
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
+      @mockWorld.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
 
       @acceptance.verifyCheckboxState(checkbox, state, callback)
       assert(callback.called)
       state = 'checked'
       element = Object.create(null, { checked: { value: false } })
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
+      @mockWorld.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
 
       assert.throws (=>
         @acceptance.verifyCheckboxState(checkbox, state, callback)),
       state = 'unchecked'
       element = Object.create(null, { checked: { value: true } })
       callback = sinon.spy()
-      world.mock.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
+      @mockWorld.expects('findElement').once().withExactArgs(checkbox, @acceptance.World.by.label).returns(element)
 
       assert.throws (=>
         @acceptance.verifyCheckboxState(checkbox, state, callback)),
     test 'should not fail if alerted with correct message', ->
       message = 'foo bar pants sock'
       callback = sinon.spy()
-      world.mock.expects('alertedWithMessage').once().withExactArgs(message).returns(true)
+      @mockWorld.expects('alertedWithMessage').once().withExactArgs(message).returns(true)
 
       @acceptance.verifyAlert(message, callback)
       assert(callback.called)
     test 'should fail if alerted not with correct message', ->
       message = 'foo bar pants sock'
       callback = sinon.spy()
-      world.mock.expects('alertedWithMessage').once().withExactArgs(message).returns(false)
+      @mockWorld.expects('alertedWithMessage').once().withExactArgs(message).returns(false)
 
       assert.throws (=>
         @acceptance.verifyAlert(message, callback)),

test/mocks/zombie-world-mock.coffee

-#this file is incredibly messy... should probably figure out a better way of handling this...
-
-
-#require our mock library
-sinon = require('sinon')
-#replace the default zombie used in files with a mocked version
-require('./zombie-mock')
-
-#find the path used to require zombie-world in production code
-worldPath = require.resolve('../../lib/worlds/zombie-world')
-#delete the cached version of this file
-delete require.cache[worldPath]
-#load in the real production world
-realWorld = require(worldPath).World
-
-mock = null
-
-#replace the cached version of the production world with our mocked version
-require.cache[worldPath] =
-  exports:
-    World: (callback) ->
-      #call the production code
-      realWorld.call @, =>
-        #create a mock for the production code and put it in module.exports
-        mock = sinon.mock(@)
-        #call our callback
-        callback()
-
-#expose the mock to the rest of the world to read
-Object.defineProperty module.exports, 'mock',
-  get: ->
-    return mock