Print panic object when using ShouldNot(Panic())
diff --git a/matchers/panic_matcher.go b/matchers/panic_matcher.go index 75ab251..1e5d4d8 100644 --- a/matchers/panic_matcher.go +++ b/matchers/panic_matcher.go
@@ -2,11 +2,14 @@ import ( "fmt" - "github.com/onsi/gomega/format" "reflect" + + "github.com/onsi/gomega/format" ) -type PanicMatcher struct{} +type PanicMatcher struct { + object interface{} +} func (matcher *PanicMatcher) Match(actual interface{}) (success bool, err error) { if actual == nil { @@ -24,6 +27,7 @@ success = false defer func() { if e := recover(); e != nil { + matcher.object = e success = true } }() @@ -38,5 +42,5 @@ } func (matcher *PanicMatcher) NegatedFailureMessage(actual interface{}) (message string) { - return format.Message(actual, "not to panic") + return format.Message(actual, fmt.Sprintf("not to panic, but panicked with <%T>: %v", matcher.object, matcher.object)) }
diff --git a/matchers/panic_matcher_test.go b/matchers/panic_matcher_test.go index 17f3935..bb58a78 100644 --- a/matchers/panic_matcher_test.go +++ b/matchers/panic_matcher_test.go
@@ -33,4 +33,13 @@ Ω(func() {}).ShouldNot(Panic()) }) }) + + Context("when assertion fails", func() { + It("should print the object passed to Panic", func() { + failuresMessages := InterceptGomegaFailures(func() { + Ω(func() { panic("ack!") }).ShouldNot(Panic()) + }) + Ω(failuresMessages).Should(ConsistOf(ContainSubstring("not to panic, but panicked with <string>: ack!"))) + }) + }) })