javascripttestingqunit

Pass parameters to a method which is expected to throw an error?


First steps with QUnit. Apologies if this is hyper-simple. I'm trying to understand things from this page of the manual.

If I have this:

QUnit.test( 'test init throws', function( assert ){
    assert.throws( loader.init, 'some message' )
});

... is there any way to pass parameters to that call to function init( param1, param2 )?


Solution

  • You can use JavaScript's bind functionality for this, which will return a function with the parameters bound to it, by doing something of the form (where here we'd be passing in arg1 and arg2):

    QUnit.test('test init throws', function(assert){
        assert.throws(loader.init.bind(loader, arg1, arg2), 'some message')
    });