tj / should.js

BDD style assertions for node.js -- test framework agnostic
MIT License
2.75k stars 195 forks source link

should.throw(): How pass argument to functions? #163

Closed diosney closed 10 years ago

diosney commented 10 years ago

Hi,

How can I pass arguments to the functions that will be tested with should.throw()?

For instance, I have the following function:

 var func = function(flag){
         if (flag) {
         throw new Error('fail');
         }
 };

What I need is to do is something like:

func(true).should.throw();
func(false).should.not.throw();
diosney commented 10 years ago

I already tested with:

    should(func(true)).throw();

But when the test is run then the Error stack is show rather than captured by should

btd commented 10 years ago

Maybe, just:

func.bind(null, true).should.throw();//?
diosney commented 10 years ago

Worked like a charm, thanks!

Can you consider adding this example to the docs?

diosney commented 10 years ago

I know its straightforward, but just in case :)

btd commented 10 years ago

Yes, no problem, i will reopen this to do not forget.

alsotang commented 10 years ago

How to pass arguments to setTimeout/process.nextTick and so on?

just

(function () {
  func(true);
}).should.throw();

is ok.

btd commented 10 years ago

Fixed in master.