testbox toThrow()

I have a Object.cfc that has a function getData():

component {
function getData(){
throw(message=“error here”, detail=“details here”);
}
}

In a ObjectTest.cfc. I have:

it( “force error”, function(){
expect( obj.getData().toThrow(type=“application”) );
});

When I do that, I’m expecting the test to pass, since I’m telling it to expect an error.

But, it fails, and goes into the “Errors” result.

How do I get that to work? Thanks!

  • Rex

Your `toThrow` should be called on `except`, not your function call.

expect( obj.getData() ).toThrow( type = "application" );

Eric is right, but there’s more. You also need to wrap your failing function in a closure:

expect( function() { obj.getData() } ).toThrow( type = “application” );

Please read the docs :slight_smile:

https://testbox.ortusbooks.com/in-depth/expectations/expecting-exceptions

Awesome, thanks!

- Rex

Thanks, that works for Lucee, but it doesn't work on ACF.

So, this same code works on Lucee 4.5.5.006 final, but not on Adobe CF
2016,0,06,308055:

describe( "CF/Lucee Test", function(){
it( "force error", function(){
expect( function(){ x = y } ).toThrow(type="expression");
});
});

It errors out with "Invalid construct: Either argument or name is missing."

If I comment out the expect line, or change it out with something like
*expect(true).toBeTrue()* it works ok

Your pr Clem is the missing semicolon in the closure

Luis Majano
CEO
Ortus Solutions, Corp

P/F: 1-888-557-8057

– -- You received this message because you are subscribed to the Google Groups “ColdBox Platform” group. For News, visit For Documentation, visit For Bug Reports, visit — You received this message because you are subscribed to the Google Groups “ColdBox Platform” group. To unsubscribe from this group and stop receiving emails from it, send an email to . To post to this group, send email to . To view this discussion on the web visit . For more options, visit .

Perfect, that was it, thanks!