errxpect

package module
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

README

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())

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

Documentation

Overview

Package errxpect supplies Gomega with assertions for mult-return value functions for simpler error testing.

A typical use of the errxpect package is by importing it into the in the current file's file block for easy reference without needing the package name.

import . "github.com/thediveo/errxpect"

Before, Gomega forced you to write assertions where you are only interested in checking the error return value of a multi-return value function:

// func multifoo() (string, int, error) { ... }

_, _, err := multifoo()
Expect(err).To(Succeed())

This can now be rewritten in a more concise form as (something which isn't allowed in stock Gomega Expect-ations):

Errxpect(multifoo()).To(HaveOccured())

ExpectWithError() needs to be slightly phrased differently, due to Golang's language restrictions, but probably even more neat now:

Errxpect(multifoo()).WithOffset(1).To(Succeed())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorAssertion added in v0.9.1

type ErrorAssertion struct {
	// contains filtered or unexported fields
}

ErrorAssertion implements the GomegaMatcher interface, while checking that actual multi-value returns are only consisting of the trailing error and all other return values must be zero.

func Errxpect

func Errxpect(actual ...interface{}) *ErrorAssertion

Errxpect wraps an actual multi-value allowing error assertions to be made on it:

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

Errxpect(foo()).To(HaveOccured())

As Golang doesn't feature automatic multi-return value passing into a varargs function if there are additional parameters present, use WithOffset() on the return value of Errxpect, such as:

Errxpect(foo()).WithOffset(1).To(Succeed())

func (*ErrorAssertion) NotTo added in v0.9.1

func (errorassertion *ErrorAssertion) NotTo(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool

NotTo makes an assertion that should not be true.

func (*ErrorAssertion) Should added in v0.9.1

func (errorassertion *ErrorAssertion) Should(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool

Should makes an assertion that should be true.

func (*ErrorAssertion) ShouldNot added in v0.9.1

func (errorassertion *ErrorAssertion) ShouldNot(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool

ShouldNot makes an assertion that should not be true.

func (*ErrorAssertion) To added in v0.9.1

func (errorassertion *ErrorAssertion) To(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool

To makes an assertion that should be true.

func (*ErrorAssertion) ToNot added in v0.9.1

func (errorassertion *ErrorAssertion) ToNot(matcher types.GomegaMatcher, optionalDescription ...interface{}) bool

ToNot makes an assertion that should not be true.

func (*ErrorAssertion) WithOffset added in v0.9.1

func (errorassertion *ErrorAssertion) WithOffset(offset int) *ErrorAssertion

WithOffset replaces ExpectWithOffset() when using Errxpect in order 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 argument of `Errxpect(...).WithOffset(offset)` appropriately.

Jump to

Keyboard shortcuts

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