/errxpect

Simplify the testing boilerplate of Gomega error expectations

Primary LanguageGoApache License 2.0Apache-2.0

errxpect

Note: Gomega as of version 1.17.0 now covers error-related assertions on functions returning multiple values. Thus, this module is not required anymore. As error-related assertions are now directly integrated, Gomega's syntax is as follows, using Error:

// was: Errxpect(Foo(42)).To(HaveOccured())
Expect(Foo(42)).Error().To(HaveOccured())

WithOffset has also been integrated into not only Expect, but also Eventually and Constantly.


PkgGoDev GitHub build and test Go Report Card

Tired?

Worn down by Gomega's noisy error testing boilerplate for function returning multiple return values? Each time, given a function returning multiple values and an error...

func Foo(int) (string, bool, error) {
    return "", false, errors.New("DOH!")
}

...Gomega forces you to do break function call and test into separate steps, requiring intermediate result variables (with most of them _s anyway):

_, _, err := Foo(42)
Expect(err).To(HaveOccured())

Errxpect!

Just import . "github.com/thediveo/errxpect" and then use Errxpect(...) in place of Expect(...). And enjoy more fluent error test assertions.

Errxpect(Foo(42)).To(HaveOccured())

As Golang doesn't unpack multiple return values automatically when there is another parameter present in a function call, error expectations with stack offsets need to the phrased as follows using .WithOffset(offset), keeping them elegant:

Errxpect(Foo(42)).WithOffset(1).To(HaveOccured())

Copyright and License

errxpect is Copyright 2020 Harald Albrecht, and licensed under the Apache License, Version 2.0.