All assertions now panic if there is no registered fail handler
diff --git a/CHANGELOG.md b/CHANGELOG.md index cbcdd08..0cfc787 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md
@@ -1,3 +1,10 @@ +## HEAD + +Improvements: + +- Added `BeSent` which attempts to send a value down a channel and fails if the attempt blocks. Can be paired with `Eventually` to safely send a value down a channel with a timeout. +- `Ω`, `Expect`, `Eventually`, and `Consistently` now immediately `panic` if there is no registered fail handler. This is always a mistake that can hide failing tests. + ## 1.0 (8/2/2014) No changes. Dropping "beta" from the version number.
diff --git a/gomega_dsl.go b/gomega_dsl.go index 4fd40f6..78bd188 100644 --- a/gomega_dsl.go +++ b/gomega_dsl.go
@@ -26,6 +26,11 @@ const GOMEGA_VERSION = "1.0" +const nilFailHandlerPanic = `You are trying to make an assertion, but Gomega's fail handler is nil. +If you're using Ginkgo then you probably forgot to put your assertion in an It(). +Alternatively, you may have forgotten to register a fail handler with RegisterFailHandler() or RegisterTestingT(). +` + var globalFailHandler types.GomegaFailHandler var defaultEventuallyTimeout = time.Second @@ -131,7 +136,7 @@ //set the first argument of `ExpectWithOffset` appropriately. func ExpectWithOffset(offset int, actual interface{}, extra ...interface{}) GomegaAssertion { if globalFailHandler == nil { - panic("Expect() called but the fail handler was nil. Did you use Expect() outside of It()?") + panic(nilFailHandlerPanic) } return assertion.New(actual, globalFailHandler, offset, extra...) } @@ -180,6 +185,9 @@ //initial argument to indicate an offset in the call stack. This is useful when building helper //functions that contain matchers. To learn more, read about `ExpectWithOffset`. func EventuallyWithOffset(offset int, actual interface{}, intervals ...interface{}) GomegaAsyncAssertion { + if globalFailHandler == nil { + panic(nilFailHandlerPanic) + } timeoutInterval := defaultEventuallyTimeout pollingInterval := defaultEventuallyPollingInterval if len(intervals) > 0 { @@ -222,6 +230,9 @@ //initial argument to indicate an offset in the call stack. This is useful when building helper //functions that contain matchers. To learn more, read about `ExpectWithOffset`. func ConsistentlyWithOffset(offset int, actual interface{}, intervals ...interface{}) GomegaAsyncAssertion { + if globalFailHandler == nil { + panic(nilFailHandlerPanic) + } timeoutInterval := defaultConsistentlyDuration pollingInterval := defaultConsistentlyPollingInterval if len(intervals) > 0 {
diff --git a/internal/assertion/assertion_test.go b/internal/assertion/assertion_test.go index f6468c1..de9eff2 100644 --- a/internal/assertion/assertion_test.go +++ b/internal/assertion/assertion_test.go
@@ -2,6 +2,7 @@ import ( "errors" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "github.com/onsi/gomega/internal/assertion" @@ -233,4 +234,19 @@ }) }) }) + + Context("Making an assertion without a registered fail handler", func() { + It("should panic", func() { + defer func() { + e := recover() + RegisterFailHandler(Fail) + if e == nil { + Fail("expected a panic to have occured") + } + }() + + RegisterFailHandler(nil) + Ω(true).Should(BeTrue()) + }) + }) })
diff --git a/internal/asyncassertion/async_assertion_test.go b/internal/asyncassertion/async_assertion_test.go index 2afd608..3b58272 100644 --- a/internal/asyncassertion/async_assertion_test.go +++ b/internal/asyncassertion/async_assertion_test.go
@@ -3,6 +3,7 @@ import ( "errors" "time" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "github.com/onsi/gomega/internal/asyncassertion" @@ -141,6 +142,23 @@ Ω(callerSkip).Should(Equal(4)) }) }) + + Context("Making an assertion without a registered fail handler", func() { + It("should panic", func() { + defer func() { + e := recover() + RegisterFailHandler(Fail) + if e == nil { + Fail("expected a panic to have occured") + } + }() + + RegisterFailHandler(nil) + c := make(chan bool, 1) + c <- true + Eventually(c).Should(Receive()) + }) + }) }) Describe("Consistently", func() { @@ -257,6 +275,22 @@ Ω(callerSkip).Should(Equal(4)) }) }) + + Context("Making an assertion without a registered fail handler", func() { + It("should panic", func() { + defer func() { + e := recover() + RegisterFailHandler(Fail) + if e == nil { + Fail("expected a panic to have occured") + } + }() + + RegisterFailHandler(nil) + c := make(chan bool) + Consistently(c).ShouldNot(Receive()) + }) + }) }) Context("when passed a function with the wrong # or arguments & returns", func() {