I'm attempting to build up a suite of QUnit tests. I am used to writing unit tests in PHPUnit, and in that environment having one test class for each class in project. A class in the testing framework serves as the base for all test classes and makes sure that all public functions on the class beginning with "test" get run. I am using CoffeeScript and QUnit, and I am attempting to build something similar.
Base test class:
- class QUnitTestCase
-
- constructor: (name) ->
- module name
- @setUp()
- @runAllTests()
-
- setUp: ->
- return null
-
- runAllTests: ->
- for funcName, func of @
- if funcName.substr(0, 4) is 'test' and typeof func is 'function'
- testName = funcName.substr(4).charAt(0).toLowerCase() + funcName.substr(5)
- @setUp()
- test testName, func()
- return null
Sample Test:
- class CanvasTest extends QUnitTestCase
-
- constructor: ->
- super "Canvas"
-
- setUp: ->
- @testObj = new Canvas()
- $('canvas').css display: 'none'
-
- testAdjust: =>
- expectedWidth = window.innerWidth
- expectedHeight = window.innerHeight
- returned = @testObj.adjust()
- strictEqual expectedWidth, returned.width, 'Returned width matches expected width'
- strictEqual expectedHeight, returned.height, 'Returned height matches expected height'
- strictEqual expectedWidth, $('#canvas').width(), 'Canvas width matches expected width'
- strictEqual expectedHeight, $('#canvas').height(), 'Canvas height matches expected height'
However, when I use this code, I get the error "Uncaught TypeError: Cannot read property 'assertions' of undefined (Line 666)" in qunit.js.
Is this a problem with the way I am attempting to write my tests, or this is an issue that should be resolved by changing QUnit? It looks as though the problem could possibly be solved by adjusting QUnit to correctly scope config when the function containing the asserts is bound to another object, but I don't know enough about the QUnit source code to truly understand what's going on inside of it.