I’m trying to build a way to do PHPUnit-style test case classes in CoffeeScript. I got very close to achieving my goal with these two classes:
QUnitTestCase.coffee:
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
CircleTest.coffee:
class CircleTest extends QUnitTestCase
constructor: ->
super "Circle"
setUp: ->
@mockCanvas = mock(Canvas)
@testObj = new Circle(@mockCanvas)
$('canvas').css display: 'none'
testDrawReturnsNull: =>
returned = @testObj.draw()
strictEqual null, returned, 'returns null'
However, QUnit blows up with this error: “Uncaught TypeError: Cannot read property ‘assertions’ of undefined (qunit.js line 666)”.
Looking at QUnit, I believe somehow the Test object inside QUnit becomes mis-scoped when the function that defines the assertions is bound to the object. If I move the call to “test()” out to the child class, like this…
testDrawReturnsNull: =>
test "drawReturnsNull", =>
returned = @testObj.draw()
strictEqual null, returned, 'returns null'
… then QUnit runs fine without any errors, but this creates a kind of duplication that I don’t really feel comfortable with. Perhaps someone who understands QUnit better can point me in the right direction.
Thanks.
You wrote
instead of
That would seem to be a problem. You want to pass the function to
testrather than running it first and passing the returned value.