I want to test a constructor, but in case some data is not provided I need to panic, how can I recover from that panic in testing?
At the moment I've added in my TestNew func a defer with recover, but in case an element from my map has an empty URL the rest will not be checked.
t.go
package testing
type test {
url string
}
func New(ops map[string]string) *test {
if ops["url"] == "" {
panic("Url missing")
}
var t = new(test)
t.url = ops["url"]
return t
}
t_test.go
package testing
type testTest map[string]string
var testingTest = []testTest {
testTest {
"url": "test",
},
testTest{
"url": "",
},
}
func NewTest(t *testing.T) {
defer func() {
recover()
}()
for _, e := range testingTest {
url := New(e)
url.hasUrl(t, e["url"])
}
}
func (s *test) hasUrl(t *testing.T, u string) {
if s.url != u {
t.Errorf("Expected %s to be equal with %s", s.url, u)
}
}