mardi 31 mars 2015

Log the thrown exception

I'm looking for a way to include the error message when the thrown exception is expected.


Here's my test:



describe('Process Text', function(){
_.each(shouldThrow, function(option){
it('throw error ('+option+')', function(){
expect(function(){
main.textValidation(option)
}).to.throw()
})
})
_.each(shouldNotThrow, function(option){
it('not throw error ('+option+')', function(){
expect(function(){
main.textValidation(option)
}).to.not.throw()
})
})
})


Here's the logged message from mocha:



✓ throw error (notify --message "Hello world" --open "https://www.holstee.com" --queue 15-04-30-16)
✓ throw error (--notify --message "Hello world" --open "https://www.holstee.com")
✓ throw error (--notify --message "Hello world" --open "https://www.holstee.com --queue "2015-04-30-16-10)
✓ not throw error (notify --message 'Hello world' --open 'https://www.holstee.com')
✓ not throw error (notify --message "Hello world" --open "https://www.holstee.com --queue 2015-04-30-16-10)


The desired output is something like this



✓ throw error
* option: notify --message "Hello world" --open "https://www.holstee.com" --queue 15-04-30-16
* error: invalid flag value `queue` should be 16 characters long
✓ throw error
* option: --notify --message "Hello world" --open "https://www.holstee.com"
* error: invalid parsed argument(s): notify
✓ throw error
* option: --notify --message "Hello world" --open "https://www.holstee.com --queue "2015-04-30-16-10
* error: invalid unparsed argument(s): 2015-04-30-16-22
✓ not throw error
* notify --message 'Hello world' --open 'https://www.holstee.com'
✓ not throw error
* notify --message "Hello world" --open "https://www.holstee.com --queue 2015-04-30-16-10


I tried this, did not work



describe('Process Text', function(){

beforeEach(function () {
test_name = this.currentTest.title;
});

_.each(shouldThrow, function(option){
it('throw error', function(){
test_name += "\n"
test_name += "* option:"
test_name += option
expect(function(){
try{
main.textValidation(option)
}catch(e){
test_name += "\n"
test_name += "* message:"
test_name += e.message
throw(e)
}
}).to.throw()
})
})
_.each(shouldNotThrow, function(option){
it('not throw error', function(){
test_name += "\n"
test_name += "* option:"
test_name += option
expect(function(){
main.textValidation(option)
}).to.not.throw()
})
})
})

Aucun commentaire:

Enregistrer un commentaire