Skip to content

Commit

Permalink
update Gomega docs to discuss GinkgoHelper()
Browse files Browse the repository at this point in the history
  • Loading branch information
onsi committed Jan 29, 2023
1 parent 6a2b3a2 commit be32774
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,24 @@ var _ = Describe("Turbo-encabulator", func() {

This makes your tests more expressive and reduces boilerplate. However, when an assertion in the helper fails the line numbers provided by Gomega are unhelpful. Instead of pointing you to the line in your test that failed, they point you the line in the helper.

To get around this, Gomega provides versions of `Expect`, `Eventually` and `Consistently` named `ExpectWithOffset`, `EventuallyWithOffset` and `ConsistentlyWithOffset` that allow you to specify an *offset* in the call stack. The offset is the first argument to these functions.
To fix this, Ginkgo and Gomega provide two options. If you are on a recent version of Ginkgo you can register your helper with Ginkgo via `GinkgoHelper()`:

```go
func assertTurboEncabulatorContains(components ...string) {
GinkgoHelper()
teComponents, err := turboEncabulator.GetComponents()
Expect(err).NotTo(HaveOccurred())

Expect(teComponents).To(HaveLen(components))
for _, component := range components {
Expect(teComponents).To(ContainElement(component))
}
}
```

now, line numbers generated by Ginkgo will skip `assertTurboEncabulatorContains` and point to the calling site instead. `GinkgoHelper()` is the recommended way to solve this problem as it allows for straightforward nesting and reuse of helper functions.

If, for some reason, you can't use `GinkgoHelper()` Gomega does provide an alternative: versions of `Expect`, `Eventually` and `Consistently` named `ExpectWithOffset`, `EventuallyWithOffset` and `ConsistentlyWithOffset` that allow you to specify an *offset* in the call stack. The offset is the first argument to these functions.

With this, we can rewrite our helper as:

Expand Down Expand Up @@ -639,6 +656,8 @@ func assertTurboEncabulatorContains(components ...string) {
}
```

Again, we recommend using `GinkgoHelper()` instead of `WithOffset(...)`.

## Provided Matchers

Gomega comes with a bunch of `GomegaMatcher`s. They're all documented here. If there's one you'd like to see written either [send a pull request or open an issue](http://github.com/onsi/gomega).
Expand Down

0 comments on commit be32774

Please sign in to comment.