golangal

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 6 Imported by: 0

README

golangal

Golangal are helpers for the excellent Ginkgo and Gomega BDD testing libraries.

See docs for available methods and example usages.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtEvery

func AtEvery(m interface{}) gomega.OmegaMatcher

AtEvery succeeds when every element in a slice matches the given matcher. Used to assert an expectation against every element in a collection.

func AtIndex

func AtIndex(idx int, m interface{}) gomega.OmegaMatcher

AtIndex succeeds when the slice element at the given index matches the given matcher.

func AtKey

func AtKey(key interface{}, m interface{}) gomega.OmegaMatcher

AtKey succeeds when the element of a slice at the given key matches the given matcher.

func EachTempDir

func EachTempDir() func() string

EachTempDir returns a function that returns a temporary directory string when invoked that is cached across a single test. The temporary directory is cleaned up after the test has finished.

Example:

tempdir := golangal.EachTempDir()
It("writes a file", func() {
  path := filepath.Join(tempdir(), "some-file.txt")
  ...
})

func EnvVars

func EnvVars() func(key, value string)

EnvVars returns a function that can be called to set an environment variable for the duration of the test, and unset it (if it did not exist) or restore the original value (if it did exist) after the test.

Example:

addEnvVar := golangal.EnvVars()
It("reads the env", func() {
  addEnvVar("MY_CONFIG_VAR", "XYZ")
  ...
})

func HaveHeader

func HaveHeader(key string, m interface{}) gomega.OmegaMatcher

func HaveJsonBody

func HaveJsonBody(m interface{}) gomega.OmegaMatcher

func HaveResponseCode

func HaveResponseCode(codeOrMatcher interface{}) gomega.OmegaMatcher

HaveResponseCode is a Gomega matcher to ensure an *httptest.ResponseRecorder has the expected response code. If it does not, the actual code and body are printed (the body is really useful information when tests fail).

func MatchCap added in v1.2.0

func MatchCap(m interface{}) gomega.OmegaMatcher

MatchCap matches the length of a collection against a matcher. It's like HaveCap, but allows a dynamic length. HaveCap(2) would be equivalent to MatchCap(Equal(2)). MatchCap also works with any type with a Cap() int method.

func MatchField

func MatchField(name string, m interface{}) gomega.OmegaMatcher

MatchField matches the value of the field named name on the actual struct. If m is a gomega matcher, it is matched against the field value. Otherwise, test against field equality.

Expect(MyStruct{Field1: 10}).To(MatchField("Field1", 10))
Expect(MyStruct{Field1: 10}).To(MatchField("Field1", BeNumerically(">", 5)))

You can match multiple fields by using SatisfyAll or And:

o := MyStruct{Field1: 10, Field2: true}
Expect(o).To(SatisfyAll(MatchField("Field1", 10), MatchField("Field2", BeTrue())))

func MatchLen

func MatchLen(m interface{}) gomega.OmegaMatcher

MatchLen matches the length of a collection against a matcher. It's like HaveLen, but allows a dynamic length. HaveLen(2) would be equivalent to MatchLen(Equal(2)). MatchLen also works with any type with a Len() int method.

func MatchPtrField

func MatchPtrField(name string, m interface{}) gomega.OmegaMatcher

MatchPtrField is same as MatchField, but the actual object should be a pointer, not a struct.

func NotError

func NotError() gomega.OmegaMatcher

NotError is like gomega's Succeed matcher, except it handles functions which return multiple values. The docs say this:

You should not use a function with multiple return values (like DoSomethingHard) with Succeed. Matchers are only passed the first value provided to Ω/Expect, the subsequent arguments are handled by Ω and Expect as outlined above. As a result of this behavior Ω(DoSomethingHard()).ShouldNot(Succeed()) would never pass.

We can circumvent this by using a noop matcher, and allowing Gomega's internal Expect behavior to assert that an error wasn't returned (this matcher will fail if it receives an error).

Expect(DoSomethingHard()).To(NotError())

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL