Documentation ¶
Overview ¶
Gomega is the Ginkgo BDD-style testing framework's preferred matcher library.
The godoc documentation describes Gomega's API. More comprehensive documentation (with examples!) is available at http://onsi.github.io/gomega/
Gomega on Github: http://github.com/onsi/gomega
Learn more about Ginkgo online: http://onsi.github.io/ginkgo
Ginkgo on Github: http://github.com/onsi/ginkgo
Gomega is MIT-Licensed
Index ¶
- Constants
- func RegisterFailHandler(handler OmegaFailHandler)
- type Actual
- type AsyncActual
- type OmegaFailHandler
- type OmegaMatcher
- func BeAssignableToTypeOf(expected interface{}) OmegaMatcher
- func BeEmpty() OmegaMatcher
- func BeEquivalentTo(expected interface{}) OmegaMatcher
- func BeFalse() OmegaMatcher
- func BeNil() OmegaMatcher
- func BeNumerically(comparator string, compareTo ...interface{}) OmegaMatcher
- func BeTrue() OmegaMatcher
- func BeZero() OmegaMatcher
- func ContainElement(element interface{}) OmegaMatcher
- func ContainSubstring(substr string, args ...interface{}) OmegaMatcher
- func Equal(expected interface{}) OmegaMatcher
- func HaveKey(key interface{}) OmegaMatcher
- func HaveLen(count int) OmegaMatcher
- func HaveOccured() OmegaMatcher
- func MatchRegexp(regexp string, args ...interface{}) OmegaMatcher
- func Panic() OmegaMatcher
Constants ¶
const GOMEGA_VERSION = "0.9"
Variables ¶
This section is empty.
Functions ¶
func RegisterFailHandler ¶
func RegisterFailHandler(handler OmegaFailHandler)
RegisterFailHandler connects Ginkgo to Gomega. When a matcher fails the fail handler passed into RegisterFailHandler is called.
Types ¶
type Actual ¶
type Actual interface { Should(matcher OmegaMatcher, optionalDescription ...interface{}) bool ShouldNot(matcher OmegaMatcher, optionalDescription ...interface{}) bool To(matcher OmegaMatcher, optionalDescription ...interface{}) bool ToNot(matcher OmegaMatcher, optionalDescription ...interface{}) bool NotTo(matcher OmegaMatcher, optionalDescription ...interface{}) bool }
Actual is returned by Ω and Expect and compares the actual value to the matcher passed to the Should/ShouldNot and To/ToNot/NotTo methods.
Typically Should/ShouldNot are used with Ω and To/ToNot/NotTo are used with Expect though this is not enforced.
All methods take a variadic optionalDescription argument. This is passed on to fmt.Sprintf() and is used to annotate failure messages.
All methods return a bool that is true if hte assertion passed and false if it failed.
Example:
Ω(farm.HasCow()).Should(BeTrue(), "Farm %v should have a cow", farm)
type AsyncActual ¶
type AsyncActual interface { Should(matcher OmegaMatcher, optionalDescription ...interface{}) bool ShouldNot(matcher OmegaMatcher, optionalDescription ...interface{}) bool }
AsyncActual is returned by Eventually and polls the actual value passed into Eventually against the matcher passed to the Should and ShouldNot methods.
Both Should and ShouldNot take a variadic optionalDescription argument. This is passed on to fmt.Sprintf() and is used to annotate failure messages. This allows you to make your failure messages more descriptive
Both Should and ShouldNot return a boolean that is true if the assertion passed and false if it failed.
Example:
Eventually(myChannel).Should(HaveLen(1), "Something should have come down the pipe.")
func Eventually ¶
func Eventually(actual interface{}, intervals ...float64) AsyncActual
Eventually wraps an actual value allowing assertions to be made on it. The assertion is tried periodically until it passes or a timeout occurs.
Both the timeout and polling interval are configurable as optional arguments: The first optional argument is the timeout in seconds expressed as a float64. The second optional argument is the polling interval in seconds expressd as a float64.
If Eventually is passed an actual that is a function taking no arguments and returning one value, then Eventually will call the function periodically and try the matcher against the function's return value.
Example:
Eventually(func() int { return thingImPolling.Count() }).Should(BeNumerically(">=", 17))
type OmegaFailHandler ¶
type OmegaMatcher ¶
All Gomega matchers must implement the OmegaMatcher interface
For details on writing custom matchers, check out: http://onsi.github.io/gomega/#adding_your_own_matchers
func BeAssignableToTypeOf ¶
func BeAssignableToTypeOf(expected interface{}) OmegaMatcher
BeAssignableToTypeOf succeeds if actual is assignable to the type of expected. It will return an error when one of the values is nil.
Ω(0).Should(BeAssignableToTypeOf(0)) // Same values Ω(5).Should(BeAssignableToTypeOf(-1)) // different values same type Ω("foo").Should(BeAssignableToTypeOf("bar")) // different values same type Ω(struct{ Foo string }{}).Should(BeAssignableToTypeOf(struct{ Foo string }{}))
func BeEmpty ¶
func BeEmpty() OmegaMatcher
BeEmpty succeeds if actual is empty. Actual must be of type string, array, map, chan, or slice.
func BeEquivalentTo ¶
func BeEquivalentTo(expected interface{}) OmegaMatcher
BeEquivalentTo is more lax than Equal, allowing equality between different types. This is done by converting actual to have the type of expected before attempting equality with reflect.DeepEqual. It is an error for actual and expected to be nil. Use BeNil() instead.
func BeNumerically ¶
func BeNumerically(comparator string, compareTo ...interface{}) OmegaMatcher
BeNumerically performs numerical assertions in a type-agnostic way. Actual and expected should be numbers, though the specific type of number is irrelevant (floa32, float64, uint8, etc...).
There are six, self-explanatory, supported comparators:
Ω(1.0).Should(BeNumerically("==", 1)) Ω(1.0).Should(BeNumerically("~", 0.999, 0.01)) Ω(1.0).Should(BeNumerically(">", 0.9)) Ω(1.0).Should(BeNumerically(">=", 1.0)) Ω(1.0).Should(BeNumerically("<", 3)) Ω(1.0).Should(BeNumerically("<=", 1.0))
func BeZero ¶
func BeZero() OmegaMatcher
BeZero succeeds if actual is the zero value for its type or if actual is nil.
func ContainElement ¶
func ContainElement(element interface{}) OmegaMatcher
ContainElement succeeds if actual contains the passed in element. By default ContainElement() uses Equal() to perform the match, however a matcher can be passed in instead:
Ω([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubstring("Bar")))
Actual must be an array, slice or map. For maps, containElement searches through the map's values.
func ContainSubstring ¶
func ContainSubstring(substr string, args ...interface{}) OmegaMatcher
ContainSubstring succeeds if actual is a string or stringer that contains the passed-in regexp. Optional arguments can be provided to construct the substring via fmt.Sprintf().
func Equal ¶
func Equal(expected interface{}) OmegaMatcher
Equal uses reflect.DeepEqual to compare actual with expected. Equal is strict about types when performing comparisons. It is an error for both actual and expected to be nil. Use BeNil() instead.
func HaveKey ¶
func HaveKey(key interface{}) OmegaMatcher
HaveKey succeeds if actual is a map with the passed in key. By default HaveKey uses Equal() to perform the match, however a matcher can be passed in instead:
Ω(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKey(MatchRegexp(`.+Foo$`)))
func HaveLen ¶
func HaveLen(count int) OmegaMatcher
HaveLen succeeds if actual has the passed-in length. Actual must be of type string, array, map, chan, or slice.
func HaveOccured ¶
func HaveOccured() OmegaMatcher
HaveOccured succeeds if actual is a non-nil error The typical Go error checking pattern looks like:
err := SomethingThatMightFail() Ω(err).ShouldNot(HaveOccured())
func MatchRegexp ¶
func MatchRegexp(regexp string, args ...interface{}) OmegaMatcher
MatchRegexp succeeds if actual is a string or stringer that matches the passed-in regexp. Optional arguments can be provided to construct a regexp via fmt.Sprintf().
func Panic ¶
func Panic() OmegaMatcher
Panic succeeds if actual is a function that, when invoked, panics. Actual must be a function that takes no arguments and returns no results.