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 And(ms ...types.GomegaMatcher) types.GomegaMatcher
- func BeADirectory() types.GomegaMatcher
- func BeARegularFile() types.GomegaMatcher
- func BeAnExistingFile() types.GomegaMatcher
- func BeAssignableToTypeOf(expected interface{}) types.GomegaMatcher
- func BeClosed() types.GomegaMatcher
- func BeElementOf(elements ...interface{}) types.GomegaMatcher
- func BeEmpty() types.GomegaMatcher
- func BeEquivalentTo(expected interface{}) types.GomegaMatcher
- func BeFalse() types.GomegaMatcher
- func BeIdenticalTo(expected interface{}) types.GomegaMatcher
- func BeNil() types.GomegaMatcher
- func BeNumerically(comparator string, compareTo ...interface{}) types.GomegaMatcher
- func BeSent(arg interface{}) types.GomegaMatcher
- func BeTemporally(comparator string, compareTo time.Time, threshold ...time.Duration) types.GomegaMatcher
- func BeTrue() types.GomegaMatcher
- func BeZero() types.GomegaMatcher
- func ConsistOf(elements ...interface{}) types.GomegaMatcher
- func ContainElement(element interface{}) types.GomegaMatcher
- func ContainElements(elements ...interface{}) types.GomegaMatcher
- func ContainSubstring(substr string, args ...interface{}) types.GomegaMatcher
- func Equal(expected interface{}) types.GomegaMatcher
- func HaveCap(count int) types.GomegaMatcher
- func HaveHTTPStatus(expected interface{}) types.GomegaMatcher
- func HaveKey(key interface{}) types.GomegaMatcher
- func HaveKeyWithValue(key interface{}, value interface{}) types.GomegaMatcher
- func HaveLen(count int) types.GomegaMatcher
- func HaveOccurred() types.GomegaMatcher
- func HavePrefix(prefix string, args ...interface{}) types.GomegaMatcher
- func HaveSuffix(suffix string, args ...interface{}) types.GomegaMatcher
- func InterceptGomegaFailures(f func()) []string
- func MatchError(expected interface{}) types.GomegaMatcher
- func MatchJSON(json interface{}) types.GomegaMatcher
- func MatchRegexp(regexp string, args ...interface{}) types.GomegaMatcher
- func MatchXML(xml interface{}) types.GomegaMatcher
- func MatchYAML(yaml interface{}) types.GomegaMatcher
- func Not(matcher types.GomegaMatcher) types.GomegaMatcher
- func Or(ms ...types.GomegaMatcher) types.GomegaMatcher
- func Panic() types.GomegaMatcher
- func PanicWith(expected interface{}) types.GomegaMatcher
- func Receive(args ...interface{}) types.GomegaMatcher
- func RegisterFailHandler(handler types.GomegaFailHandler)
- func RegisterFailHandlerWithT(t types.TWithHelper, handler types.GomegaFailHandler)
- func RegisterTestingT(t types.GomegaTestingT)
- func SatisfyAll(matchers ...types.GomegaMatcher) types.GomegaMatcher
- func SatisfyAny(matchers ...types.GomegaMatcher) types.GomegaMatcher
- func SetDefaultConsistentlyDuration(t time.Duration)
- func SetDefaultConsistentlyPollingInterval(t time.Duration)
- func SetDefaultEventuallyPollingInterval(t time.Duration)
- func SetDefaultEventuallyTimeout(t time.Duration)
- func Succeed() types.GomegaMatcher
- func WithTransform(transform interface{}, matcher types.GomegaMatcher) types.GomegaMatcher
- type Assertion
- type AsyncAssertion
- func Consistently(actual interface{}, intervals ...interface{}) AsyncAssertion
- func ConsistentlyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion
- func Eventually(actual interface{}, intervals ...interface{}) AsyncAssertion
- func EventuallyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion
- type Gomega
- type GomegaAssertion
- type GomegaAsyncAssertion
- type GomegaWithT
- type OmegaMatcher
- type WithT
- func (g *WithT) Consistently(actual interface{}, intervals ...interface{}) AsyncAssertion
- func (g *WithT) ConsistentlyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion
- func (g *WithT) Eventually(actual interface{}, intervals ...interface{}) AsyncAssertion
- func (g *WithT) EventuallyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion
- func (g *WithT) Expect(actual interface{}, extra ...interface{}) Assertion
- func (g *WithT) ExpectWithOffset(offset int, actual interface{}, extra ...interface{}) Assertion
Constants ¶
const GOMEGA_VERSION = "1.10.2"
Variables ¶
This section is empty.
Functions ¶
func And ¶
func And(ms ...types.GomegaMatcher) types.GomegaMatcher
And succeeds only if all of the given matchers succeed. The matchers are tried in order, and will fail-fast if one doesn't succeed.
Expect("hi").To(And(HaveLen(2), Equal("hi"))
And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
func BeADirectory ¶
func BeADirectory() types.GomegaMatcher
BeADirectory succeeds if a file exists and is a directory. Actual must be a string representing the abs path to the file being checked.
func BeARegularFile ¶
func BeARegularFile() types.GomegaMatcher
BeARegularFile succeeds if a file exists and is a regular file. Actual must be a string representing the abs path to the file being checked.
func BeAnExistingFile ¶
func BeAnExistingFile() types.GomegaMatcher
BeAnExistingFile succeeds if a file exists. Actual must be a string representing the abs path to the file being checked.
func BeAssignableToTypeOf ¶
func BeAssignableToTypeOf(expected interface{}) types.GomegaMatcher
BeAssignableToTypeOf succeeds if actual is assignable to the type of expected. It will return an error when one of the values is nil.
Expect(0).Should(BeAssignableToTypeOf(0)) // Same values Expect(5).Should(BeAssignableToTypeOf(-1)) // different values same type Expect("foo").Should(BeAssignableToTypeOf("bar")) // different values same type Expect(struct{ Foo string }{}).Should(BeAssignableToTypeOf(struct{ Foo string }{}))
func BeClosed ¶
func BeClosed() types.GomegaMatcher
BeClosed succeeds if actual is a closed channel. It is an error to pass a non-channel to BeClosed, it is also an error to pass nil
In order to check whether or not the channel is closed, Gomega must try to read from the channel (even in the `ShouldNot(BeClosed())` case). You should keep this in mind if you wish to make subsequent assertions about values coming down the channel.
Also, if you are testing that a *buffered* channel is closed you must first read all values out of the channel before asserting that it is closed (it is not possible to detect that a buffered-channel has been closed until all its buffered values are read).
Finally, as a corollary: it is an error to check whether or not a send-only channel is closed.
func BeElementOf ¶ added in v1.6.0
func BeElementOf(elements ...interface{}) types.GomegaMatcher
BeElementOf succeeds if actual is contained in the passed in elements. BeElementOf() always uses Equal() to perform the match. When the passed in elements are comprised of a single element that is either an Array or Slice, BeElementOf() behaves as the reverse of ContainElement() that operates with Equal() to perform the match.
Expect(2).Should(BeElementOf([]int{1, 2})) Expect(2).Should(BeElementOf([2]int{1, 2}))
Otherwise, BeElementOf() provides a syntactic sugar for Or(Equal(_), Equal(_), ...):
Expect(2).Should(BeElementOf(1, 2))
Actual must be typed.
func BeEmpty ¶
func BeEmpty() types.GomegaMatcher
BeEmpty succeeds if actual is empty. Actual must be of type string, array, map, chan, or slice.
func BeEquivalentTo ¶
func BeEquivalentTo(expected interface{}) types.GomegaMatcher
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 BeIdenticalTo ¶
func BeIdenticalTo(expected interface{}) types.GomegaMatcher
BeIdenticalTo uses the == operator to compare actual with expected. BeIdenticalTo is strict about types when performing comparisons. It is an error for both actual and expected to be nil. Use BeNil() instead.
func BeNumerically ¶
func BeNumerically(comparator string, compareTo ...interface{}) types.GomegaMatcher
BeNumerically performs numerical assertions in a type-agnostic way. Actual and expected should be numbers, though the specific type of number is irrelevant (float32, float64, uint8, etc...).
There are six, self-explanatory, supported comparators:
Expect(1.0).Should(BeNumerically("==", 1)) Expect(1.0).Should(BeNumerically("~", 0.999, 0.01)) Expect(1.0).Should(BeNumerically(">", 0.9)) Expect(1.0).Should(BeNumerically(">=", 1.0)) Expect(1.0).Should(BeNumerically("<", 3)) Expect(1.0).Should(BeNumerically("<=", 1.0))
func BeSent ¶
func BeSent(arg interface{}) types.GomegaMatcher
BeSent succeeds if a value can be sent to actual. Actual must be a channel (and cannot be a receive-only channel) that can sent the type of the value passed into BeSent -- anything else is an error. In addition, actual must not be closed.
BeSent never blocks:
- If the channel `c` is not ready to receive then Expect(c).Should(BeSent("foo")) will fail immediately - If the channel `c` is eventually ready to receive then Eventually(c).Should(BeSent("foo")) will succeed.. presuming the channel becomes ready to receive before Eventually's timeout - If the channel `c` is closed then Expect(c).Should(BeSent("foo")) and Ω(c).ShouldNot(BeSent("foo")) will both fail immediately
Of course, the value is actually sent to the channel. The point of `BeSent` is less to make an assertion about the availability of the channel (which is typically an implementation detail that your test should not be concerned with). Rather, the point of `BeSent` is to make it possible to easily and expressively write tests that can timeout on blocked channel sends.
func BeTemporally ¶
func BeTemporally(comparator string, compareTo time.Time, threshold ...time.Duration) types.GomegaMatcher
BeTemporally compares time.Time's like BeNumerically Actual and expected must be time.Time. The comparators are the same as for BeNumerically
Expect(time.Now()).Should(BeTemporally(">", time.Time{})) Expect(time.Now()).Should(BeTemporally("~", time.Now(), time.Second))
func BeZero ¶
func BeZero() types.GomegaMatcher
BeZero succeeds if actual is the zero value for its type or if actual is nil.
func ConsistOf ¶
func ConsistOf(elements ...interface{}) types.GomegaMatcher
ConsistOf succeeds if actual contains precisely the elements passed into the matcher. The ordering of the elements does not matter. By default ConsistOf() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:
Expect([]string{"Foo", "FooBar"}).Should(ConsistOf("FooBar", "Foo")) Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Bar"), "Foo")) Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Foo"), ContainSubstring("Foo")))
Actual must be an array, slice or map. For maps, ConsistOf matches against the map's values.
You typically pass variadic arguments to ConsistOf (as in the examples above). However, if you need to pass in a slice you can provided that it is the only element passed in to ConsistOf:
Expect([]string{"Foo", "FooBar"}).Should(ConsistOf([]string{"FooBar", "Foo"}))
Note that Go's type system does not allow you to write this as ConsistOf([]string{"FooBar", "Foo"}...) as []string and []interface{} are different types - hence the need for this special rule.
func ContainElement ¶
func ContainElement(element interface{}) types.GomegaMatcher
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:
Expect([]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 ContainElements ¶ added in v1.9.0
func ContainElements(elements ...interface{}) types.GomegaMatcher
ContainElements succeeds if actual contains the passed in elements. The ordering of the elements does not matter. By default ContainElements() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:
Expect([]string{"Foo", "FooBar"}).Should(ContainElements("FooBar")) Expect([]string{"Foo", "FooBar"}).Should(ContainElements(ContainSubstring("Bar"), "Foo"))
Actual must be an array, slice or map. For maps, ContainElements searches through the map's values.
func ContainSubstring ¶
func ContainSubstring(substr string, args ...interface{}) types.GomegaMatcher
ContainSubstring succeeds if actual is a string or stringer that contains the passed-in substring. Optional arguments can be provided to construct the substring via fmt.Sprintf().
func Equal ¶
func Equal(expected interface{}) types.GomegaMatcher
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 HaveCap ¶
func HaveCap(count int) types.GomegaMatcher
HaveCap succeeds if actual has the passed-in capacity. Actual must be of type array, chan, or slice.
func HaveHTTPStatus ¶ added in v1.10.0
func HaveHTTPStatus(expected interface{}) types.GomegaMatcher
HaveHTTPStatus succeeds if the Status or StatusCode field of an HTTP response matches. Actual must be either a *http.Response or *httptest.ResponseRecorder. Expected must be either an int or a string.
Expect(resp).Should(HaveHTTPStatus(http.StatusOK)) // asserts that resp.StatusCode == 200 Expect(resp).Should(HaveHTTPStatus("404 Not Found")) // asserts that resp.Status == "404 Not Found"
func HaveKey ¶
func HaveKey(key interface{}) types.GomegaMatcher
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:
Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKey(MatchRegexp(`.+Foo$`)))
func HaveKeyWithValue ¶
func HaveKeyWithValue(key interface{}, value interface{}) types.GomegaMatcher
HaveKeyWithValue succeeds if actual is a map with the passed in key and value. By default HaveKeyWithValue uses Equal() to perform the match, however a matcher can be passed in instead:
Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue("Foo", "Bar")) Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue(MatchRegexp(`.+Foo$`), "Bar"))
func HaveLen ¶
func HaveLen(count int) types.GomegaMatcher
HaveLen succeeds if actual has the passed-in length. Actual must be of type string, array, map, chan, or slice.
func HaveOccurred ¶
func HaveOccurred() types.GomegaMatcher
HaveOccurred succeeds if actual is a non-nil error The typical Go error checking pattern looks like:
err := SomethingThatMightFail() Expect(err).ShouldNot(HaveOccurred())
func HavePrefix ¶
func HavePrefix(prefix string, args ...interface{}) types.GomegaMatcher
HavePrefix succeeds if actual is a string or stringer that contains the passed-in string as a prefix. Optional arguments can be provided to construct via fmt.Sprintf().
func HaveSuffix ¶
func HaveSuffix(suffix string, args ...interface{}) types.GomegaMatcher
HaveSuffix succeeds if actual is a string or stringer that contains the passed-in string as a suffix. Optional arguments can be provided to construct via fmt.Sprintf().
func InterceptGomegaFailures ¶
func InterceptGomegaFailures(f func()) []string
InterceptGomegaFailures runs a given callback and returns an array of failure messages generated by any Gomega assertions within the callback.
This is accomplished by temporarily replacing the *global* fail handler with a fail handler that simply annotates failures. The original fail handler is reset when InterceptGomegaFailures returns.
This is most useful when testing custom matchers, but can also be used to check on a value using a Gomega assertion without causing a test failure.
func MatchError ¶
func MatchError(expected interface{}) types.GomegaMatcher
MatchError succeeds if actual is a non-nil error that matches the passed in string/error.
These are valid use-cases:
Expect(err).Should(MatchError("an error")) //asserts that err.Error() == "an error" Expect(err).Should(MatchError(SomeError)) //asserts that err == SomeError (via reflect.DeepEqual)
It is an error for err to be nil or an object that does not implement the Error interface
func MatchJSON ¶
func MatchJSON(json interface{}) types.GomegaMatcher
MatchJSON succeeds if actual is a string or stringer of JSON that matches the expected JSON. The JSONs are decoded and the resulting objects are compared via reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.
func MatchRegexp ¶
func MatchRegexp(regexp string, args ...interface{}) types.GomegaMatcher
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 MatchXML ¶ added in v1.2.0
func MatchXML(xml interface{}) types.GomegaMatcher
MatchXML succeeds if actual is a string or stringer of XML that matches the expected XML. The XMLs are decoded and the resulting objects are compared via reflect.DeepEqual so things like whitespaces shouldn't matter.
func MatchYAML ¶
func MatchYAML(yaml interface{}) types.GomegaMatcher
MatchYAML succeeds if actual is a string or stringer of YAML that matches the expected YAML. The YAML's are decoded and the resulting objects are compared via reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.
func Not ¶
func Not(matcher types.GomegaMatcher) types.GomegaMatcher
Not negates the given matcher; it succeeds if the given matcher fails.
Expect(1).To(Not(Equal(2))
And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
func Or ¶
func Or(ms ...types.GomegaMatcher) types.GomegaMatcher
Or succeeds if any of the given matchers succeed. The matchers are tried in order and will return immediately upon the first successful match.
Expect("hi").To(Or(HaveLen(3), HaveLen(2))
And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
func Panic ¶
func Panic() types.GomegaMatcher
Panic succeeds if actual is a function that, when invoked, panics. Actual must be a function that takes no arguments and returns no results.
func PanicWith ¶ added in v1.10.0
func PanicWith(expected interface{}) types.GomegaMatcher
PanicWith succeeds if actual is a function that, when invoked, panics with a specific value. Actual must be a function that takes no arguments and returns no results.
By default PanicWith uses Equal() to perform the match, however a matcher can be passed in instead:
Expect(fn).Should(PanicWith(MatchRegexp(`.+Foo$`)))
func Receive ¶
func Receive(args ...interface{}) types.GomegaMatcher
Receive succeeds if there is a value to be received on actual. Actual must be a channel (and cannot be a send-only channel) -- anything else is an error.
Receive returns immediately and never blocks:
- If there is nothing on the channel `c` then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.
- If the channel `c` is closed then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.
- If there is something on the channel `c` ready to be read, then Expect(c).Should(Receive()) will pass and Ω(c).ShouldNot(Receive()) will fail.
If you have a go-routine running in the background that will write to channel `c` you can:
Eventually(c).Should(Receive())
This will timeout if nothing gets sent to `c` (you can modify the timeout interval as you normally do with `Eventually`)
A similar use-case is to assert that no go-routine writes to a channel (for a period of time). You can do this with `Consistently`:
Consistently(c).ShouldNot(Receive())
You can pass `Receive` a matcher. If you do so, it will match the received object against the matcher. For example:
Expect(c).Should(Receive(Equal("foo")))
When given a matcher, `Receive` will always fail if there is nothing to be received on the channel.
Passing Receive a matcher is especially useful when paired with Eventually:
Eventually(c).Should(Receive(ContainSubstring("bar")))
will repeatedly attempt to pull values out of `c` until a value matching "bar" is received.
Finally, if you want to have a reference to the value *sent* to the channel you can pass the `Receive` matcher a pointer to a variable of the appropriate type:
var myThing thing Eventually(thingChan).Should(Receive(&myThing)) Expect(myThing.Sprocket).Should(Equal("foo")) Expect(myThing.IsValid()).Should(BeTrue())
func RegisterFailHandler ¶
func RegisterFailHandler(handler types.GomegaFailHandler)
RegisterFailHandler connects Ginkgo to Gomega. When a matcher fails the fail handler passed into RegisterFailHandler is called.
func RegisterFailHandlerWithT ¶ added in v1.4.3
func RegisterFailHandlerWithT(t types.TWithHelper, handler types.GomegaFailHandler)
RegisterFailHandlerWithT ensures that the given types.TWithHelper and fail handler are used globally.
func RegisterTestingT ¶
func RegisterTestingT(t types.GomegaTestingT)
RegisterTestingT connects Gomega to Golang's XUnit style Testing.T tests. It is now deprecated and you should use NewWithT() instead.
Legacy Documentation:
You'll need to call this at the top of each XUnit style test:
func TestFarmHasCow(t *testing.T) { RegisterTestingT(t) f := farm.New([]string{"Cow", "Horse"}) Expect(f.HasCow()).To(BeTrue(), "Farm should have cow") }
Note that this *testing.T is registered *globally* by Gomega (this is why you don't have to pass `t` down to the matcher itself). This means that you cannot run the XUnit style tests in parallel as the global fail handler cannot point to more than one testing.T at a time.
NewWithT() does not have this limitation ¶
(As an aside: Ginkgo gets around this limitation by running parallel tests in different *processes*).
func SatisfyAll ¶
func SatisfyAll(matchers ...types.GomegaMatcher) types.GomegaMatcher
SatisfyAll is an alias for And().
Expect("hi").Should(SatisfyAll(HaveLen(2), Equal("hi")))
func SatisfyAny ¶
func SatisfyAny(matchers ...types.GomegaMatcher) types.GomegaMatcher
SatisfyAny is an alias for Or().
Expect("hi").SatisfyAny(Or(HaveLen(3), HaveLen(2))
func SetDefaultConsistentlyDuration ¶
SetDefaultConsistentlyDuration sets the default duration for Consistently. Consistently will verify that your condition is satisfied for this long.
func SetDefaultConsistentlyPollingInterval ¶
SetDefaultConsistentlyPollingInterval sets the default polling interval for Consistently.
func SetDefaultEventuallyPollingInterval ¶
SetDefaultEventuallyPollingInterval sets the default polling interval for Eventually.
func SetDefaultEventuallyTimeout ¶
SetDefaultEventuallyTimeout sets the default timeout duration for Eventually. Eventually will repeatedly poll your condition until it succeeds, or until this timeout elapses.
func Succeed ¶
func Succeed() types.GomegaMatcher
Succeed passes if actual is a nil error Succeed is intended to be used with functions that return a single error value. Instead of
err := SomethingThatMightFail() Expect(err).ShouldNot(HaveOccurred())
You can write:
Expect(SomethingThatMightFail()).Should(Succeed())
It is a mistake to use Succeed with a function that has multiple return values. Gomega's Ω and Expect functions automatically trigger failure if any return values after the first return value are non-zero/non-nil. This means that Ω(MultiReturnFunc()).ShouldNot(Succeed()) can never pass.
func WithTransform ¶
func WithTransform(transform interface{}, matcher types.GomegaMatcher) types.GomegaMatcher
WithTransform applies the `transform` to the actual value and matches it against `matcher`. The given transform must be a function of one parameter that returns one value.
var plus1 = func(i int) int { return i + 1 } Expect(1).To(WithTransform(plus1, Equal(2))
And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
Types ¶
type Assertion ¶ added in v1.5.0
type Assertion interface { Should(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool ShouldNot(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool To(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool ToNot(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool NotTo(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool }
Assertion 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 argument allows you to make your failure messages more descriptive. If a single argument of type `func() string` is passed, this function will be lazily evaluated if a failure occurs and the returned string is used to annotate the failure message. Otherwise, this argument is passed on to fmt.Sprintf() and then used to annotate the failure message.
All methods return a bool that is true if the assertion passed and false if it failed.
Example:
Ω(farm.HasCow()).Should(BeTrue(), "Farm %v should have a cow", farm)
func Expect ¶
func Expect(actual interface{}, extra ...interface{}) Assertion
Expect wraps an actual value allowing assertions to be made on it:
Expect("foo").To(Equal("foo"))
If Expect is passed more than one argument it will pass the *first* argument to the matcher. All subsequent arguments will be required to be nil/zero.
This is convenient if you want to make an assertion on a method/function that returns a value and an error - a common patter in Go.
For example, given a function with signature:
func MyAmazingThing() (int, error)
Then:
Expect(MyAmazingThing()).Should(Equal(3))
Will succeed only if `MyAmazingThing()` returns `(3, nil)`
Expect and Ω are identical
func ExpectWithOffset ¶
ExpectWithOffset wraps an actual value allowing assertions to be made on it:
ExpectWithOffset(1, "foo").To(Equal("foo"))
Unlike `Expect` and `Ω`, `ExpectWithOffset` takes an additional integer argument that is used to modify the call-stack offset when computing line numbers.
This is most useful in helper functions that make assertions. If you want Gomega's error message to refer to the calling line in the test (as opposed to the line in the helper function) set the first argument of `ExpectWithOffset` appropriately.
func Ω ¶
func Ω(actual interface{}, extra ...interface{}) Assertion
Ω wraps an actual value allowing assertions to be made on it:
Ω("foo").Should(Equal("foo"))
If Ω is passed more than one argument it will pass the *first* argument to the matcher. All subsequent arguments will be required to be nil/zero.
This is convenient if you want to make an assertion on a method/function that returns a value and an error - a common patter in Go.
For example, given a function with signature:
func MyAmazingThing() (int, error)
Then:
Ω(MyAmazingThing()).Should(Equal(3))
Will succeed only if `MyAmazingThing()` returns `(3, nil)`
Ω and Expect are identical
type AsyncAssertion ¶ added in v1.5.0
type AsyncAssertion interface { Should(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool ShouldNot(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool }
AsyncAssertion is returned by Eventually and Consistently 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 argument allows you to make your failure messages more descriptive. If a single argument of type `func() string` is passed, this function will be lazily evaluated if a failure occurs and the returned string is used to annotate the failure message. Otherwise, this argument is passed on to fmt.Sprintf() and then used to annotate the failure message.
Both Should and ShouldNot return a boolean that is true if the assertion passed and false if it failed.
Example:
Eventually(myChannel).Should(Receive(), "Something should have come down the pipe.") Consistently(myChannel).ShouldNot(Receive(), func() string { return "Nothing should have come down the pipe." })
func Consistently ¶
func Consistently(actual interface{}, intervals ...interface{}) AsyncAssertion
Consistently wraps an actual value allowing assertions to be made on it. The assertion is tried periodically and is required to pass for a period of time.
Both the total time and polling interval are configurable as optional arguments: The first optional argument is the duration that Consistently will run for The second optional argument is the polling interval
Both intervals can either be specified as time.Duration, parsable duration strings or as floats/integers. In the last case they are interpreted as seconds.
If Consistently is passed an actual that is a function taking no arguments and returning at least one value, then Consistently will call the function periodically and try the matcher against the function's first return value.
If the function returns more than one value, then Consistently will pass the first value to the matcher and assert that all other values are nil/zero. This allows you to pass Consistently a function that returns a value and an error - a common pattern in Go.
Consistently is useful in cases where you want to assert that something *does not happen* over a period of time. For example, you want to assert that a goroutine does *not* send data down a channel. In this case, you could:
Consistently(channel).ShouldNot(Receive())
Consistently's default duration is 100ms, and its default polling interval is 10ms
func ConsistentlyWithOffset ¶
func ConsistentlyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion
ConsistentlyWithOffset operates like Consistently but takes an additional 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 Eventually ¶
func Eventually(actual interface{}, intervals ...interface{}) AsyncAssertion
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 The second optional argument is the polling interval
Both intervals can either be specified as time.Duration, parsable duration strings or as floats/integers. In the last case they are interpreted as seconds.
If Eventually is passed an actual that is a function taking no arguments and returning at least one value, then Eventually will call the function periodically and try the matcher against the function's first return value.
Example:
Eventually(func() int { return thingImPolling.Count() }).Should(BeNumerically(">=", 17))
Note that this example could be rewritten:
Eventually(thingImPolling.Count).Should(BeNumerically(">=", 17))
If the function returns more than one value, then Eventually will pass the first value to the matcher and assert that all other values are nil/zero. This allows you to pass Eventually a function that returns a value and an error - a common pattern in Go.
For example, consider a method that returns a value and an error:
func FetchFromDB() (string, error)
Then
Eventually(FetchFromDB).Should(Equal("hasselhoff"))
Will pass only if the the returned error is nil and the returned string passes the matcher.
Eventually's default timeout is 1 second, and its default polling interval is 10ms
func EventuallyWithOffset ¶
func EventuallyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion
EventuallyWithOffset operates like Eventually but takes an additional 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`.
type Gomega ¶ added in v1.10.0
type Gomega interface { Expect(actual interface{}, extra ...interface{}) Assertion Eventually(actual interface{}, intervals ...interface{}) AsyncAssertion Consistently(actual interface{}, intervals ...interface{}) AsyncAssertion }
Gomega describes the essential Gomega DSL. This interface allows libraries to abstract between the standard package-level function implementations and alternatives like *WithT.
var Default Gomega = globalFailHandlerGomega{}
DefaultGomega supplies the standard package-level implementation
type GomegaAssertion ¶
type GomegaAssertion = Assertion
GomegaAssertion is deprecated in favor of Assertion, which does not stutter.
type GomegaAsyncAssertion ¶
type GomegaAsyncAssertion = AsyncAssertion
GomegaAsyncAssertion is deprecated in favor of AsyncAssertion, which does not stutter.
type GomegaWithT ¶ added in v1.3.0
type GomegaWithT = WithT
GomegaWithT is deprecated in favor of gomega.WithT, which does not stutter.
func NewGomegaWithT ¶ added in v1.3.0
func NewGomegaWithT(t types.GomegaTestingT) *GomegaWithT
NewGomegaWithT is deprecated in favor of gomega.NewWithT, which does not stutter.
type OmegaMatcher ¶
type OmegaMatcher types.GomegaMatcher
OmegaMatcher is deprecated in favor of the better-named and better-organized types.GomegaMatcher but sticks around to support existing code that uses it
type WithT ¶ added in v1.5.0
type WithT struct {
// contains filtered or unexported fields
}
WithT wraps a *testing.T and provides `Expect`, `Eventually`, and `Consistently` methods. This allows you to leverage Gomega's rich ecosystem of matchers in standard `testing` test suites.
Use `NewWithT` to instantiate a `WithT`
func NewWithT ¶ added in v1.5.0
func NewWithT(t types.GomegaTestingT) *WithT
NewWithT takes a *testing.T and returngs a `gomega.WithT` allowing you to use `Expect`, `Eventually`, and `Consistently` along with Gomega's rich ecosystem of matchers in standard `testing` test suits.
func TestFarmHasCow(t *testing.T) { g := gomega.NewWithT(t) f := farm.New([]string{"Cow", "Horse"}) g.Expect(f.HasCow()).To(BeTrue(), "Farm should have cow") }
func (*WithT) Consistently ¶ added in v1.5.0
func (g *WithT) Consistently(actual interface{}, intervals ...interface{}) AsyncAssertion
Consistently is used to make asynchronous assertions. See documentation for Consistently.
func (*WithT) ConsistentlyWithOffset ¶ added in v1.10.2
func (g *WithT) ConsistentlyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion
ConsistentlyWithOffset is used to make asynchronous assertions. See documentation for ConsistentlyWithOffset.
func (*WithT) Eventually ¶ added in v1.5.0
func (g *WithT) Eventually(actual interface{}, intervals ...interface{}) AsyncAssertion
Eventually is used to make asynchronous assertions. See documentation for Eventually.
func (*WithT) EventuallyWithOffset ¶ added in v1.10.2
func (g *WithT) EventuallyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncAssertion
EventuallyWithOffset is used to make asynchronous assertions. See documentation for EventuallyWithOffset.
func (*WithT) Expect ¶ added in v1.5.0
Expect is used to make assertions. See documentation for Expect.
func (*WithT) ExpectWithOffset ¶ added in v1.10.2
ExpectWithOffset is used to make assertions. See documentation for ExpectWithOffset.
Directories ¶
Path | Synopsis |
---|---|
Package gbytes provides a buffer that supports incrementally detecting input.
|
Package gbytes provides a buffer that supports incrementally detecting input. |
protobuf
Package protobuf is a generated protocol buffer package.
|
Package protobuf is a generated protocol buffer package. |
internal
|
|