I'm using tape and I'm trying to test a couple of functions. My functions throw errors and validate objects. I like throwing errors because then later my promises can catch them. I'm trying to run simple tests and establishing the data
argument in all the scenarios to hit each error in the stack. How can I test this function without putting it in a try / catch
every time? I see there's two functions in the API t.throws()
and t.doesNotThrow()
, I've tried them both and even added the extra params like t.throws(myFunc({}), Error, "no data")
but nothing seems to work as expected.
var test = require('tape')
var _ = require('underscore')
function myFunction(data){
if(!data) throw new Error("no data")
if(_.size(data) == 0) throw new Error("data is empty")
if(!data.date) throw new Error("no data date")
if(!data.messages.length == 0) throw new Error("no messages")
data.cake = "is a lie"
return data
}
test("my function", function(t){
t.throws(myFunction({}))
t.end()
}
I have no loyalty to tape, and I have no clue what I'm doing. I just want to simply test functions synchronous that throw exceptions, without a ton of overhead. So if there's a better unit-testing framework for this use case I'd be glad to use it. If tape has this ability I'd love to use it.
Is this how it should be done?
test("my function", function(t){
try{
myFunction({})
t.fail()
}catch(e){
t.pass(e.message)
}
t.end()
})
Aucun commentaire:
Enregistrer un commentaire