Github.com/stretchr/testify: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 54: Line 54:


The programming model requires to:
The programming model requires to:
* define such a mock instance
* [[#Defining_the_Mock|define]] such a mock instance
* instantiate it with <code>new()</code> or <code>&SomeInterfaceMock{}</code>
* instantiate it with <code>new()</code> or <code>&SomeInterfaceMock{}</code>
* configure its behavior, by configuring its responses to method invocations (Testify calls this "setting up expectations")
* configure its behavior, by configuring its responses to method invocations (Testify calls this "setting up expectations")
Line 61: Line 61:
* ensure the code behave correctly, knowing that the mock returned what we instructed it to return
* ensure the code behave correctly, knowing that the mock returned what we instructed it to return
* optionally, "assert expectations" on the mock.
* optionally, "assert expectations" on the mock.
===Defining the Mock===
It's a good idea to encapsulate the mock definition(s) in a package-level "*_mocks_test.go" file. If we're testing <code>mypkg</code> package, then the code lives in the <code>mypkg.go</code> file, the tests live in <code>mypkg_test.go</code> file and the mocks live in <code>mypkg_mocks_test.go</code>:
<font size=-2>
.
└── internal
     └── mypkg
        ├── mypkg.go
       ├── mypkg_test.go
       └── mypkg_mocks_test.go
</font>





Revision as of 21:35, 6 March 2024

External

Internal

Overview

Installation

go get github.com/stretchr/testify

Programming Model

Assertions

package yours

import (
  "testing"
  tassert "github.com/stretchr/testify/assert"
)

func TestSomething(t *testing.T) {

  assert := tassert.New(t)

  // assert equality
  assert.Equal(123, 123, "they should be equal")

  // assert inequality
  assert.NotEqual(123, 456, "they should not be equal")

  // assert for nil (good for errors)
  assert.Nil(object)

  // assert for not nil (good when you expect something)
  if assert.NotNil(object) {

    // now we know that object isn't nil, we are safe to make
    // further assertions without causing any errors
    assert.Equal("Something", object.Value)
  }
}

To check that an error has the expected message:

err := ...
assert.NotNil(err)
assert.Equal(err.Error(), "expected message")

Mocks

https://pkg.go.dev/github.com/stretchr/testify/mock

Mocking with Testify is based on the assumption that we want to construct mock instances to replace in testing real instances, standing in for external dependencies, defined by interfaces. '

The programming model requires to:

  • define such a mock instance
  • instantiate it with new() or &SomeInterfaceMock{}
  • configure its behavior, by configuring its responses to method invocations (Testify calls this "setting up expectations")
  • pass it to the code that needs to be tested
  • run the code that needs to be tested
  • ensure the code behave correctly, knowing that the mock returned what we instructed it to return
  • optionally, "assert expectations" on the mock.

Defining the Mock

It's a good idea to encapsulate the mock definition(s) in a package-level "*_mocks_test.go" file. If we're testing mypkg package, then the code lives in the mypkg.go file, the tests live in mypkg_test.go file and the mocks live in mypkg_mocks_test.go:

.
└── internal
    └── mypkg
        ├── mypkg.go
        ├── mypkg_test.go
        └── mypkg_mocks_test.go


...
type MockMyService struct {
	mock.Mock
}

func (m * MockMyService) Start() error {
  ...
}

...

mockMyService := new(MockMyService)