Skip to content

Commit

Permalink
test: add example of mocking external types
Browse files Browse the repository at this point in the history
I realised that when I switched to using `packages.Load()` I actually added the ability to mock external types (i.e. types that aren't part of your own source tree). I've just added an example test and updated the README.
  • Loading branch information
adamconnelly committed Jun 2, 2024
1 parent 19e80b1 commit 3e373d9
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ At the moment Kelpie is very much in development, and there are missing features

The following is a list of known-outstanding features:

- [ ] Generating mocks for external (i.e. not in your source tree) interfaces by name.
- [ ] Generating mocks for inline interfaces in structs.

## Quickstart
Expand Down Expand Up @@ -206,6 +205,32 @@ t.True(mock.Called(registrationservice.Register(kelpie.Any[string]()).Times(2)))
t.True(mock.Called(registrationservice.Register("Wendy").Never()))
```

### Mocking an interface from an external package

Kelpie can happily mock interfaces that aren't part of your own source. You don't need to do anything special to mock an "external" interface - just specify the package and interface name you want to mock:

```go
//go:generate go run ../cmd/kelpie generate --package io --interfaces Reader

func (t *ExternalTypesTests) Test_CanMockAnExternalType() {
// Arrange
var bytesRead []byte
mock := reader.NewMock()
mock.Setup(reader.Read(kelpie.Match(func(b []byte) bool {
bytesRead = b
return true
})).Return(20, nil))

// Act
read, err := mock.Instance().Read([]byte("Hello World!"))

// Assert
t.NoError(err)
t.Equal(20, read)
t.Equal([]byte("Hello World!"), bytesRead)
}
```

## FAQ

### What makes Kelpie so magical
Expand Down
37 changes: 37 additions & 0 deletions examples/external_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package examples

import (
"testing"

"github.com/adamconnelly/kelpie"
"github.com/adamconnelly/kelpie/examples/mocks/reader"
"github.com/stretchr/testify/suite"
)

//go:generate go run ../cmd/kelpie generate --package io --interfaces Reader

type ExternalTypesTests struct {
suite.Suite
}

func (t *ExternalTypesTests) Test_CanMockAnExternalType() {
// Arrange
var bytesRead []byte
mock := reader.NewMock()
mock.Setup(reader.Read(kelpie.Match(func(b []byte) bool {
bytesRead = b
return true
})).Return(20, nil))

// Act
read, err := mock.Instance().Read([]byte("Hello World!"))

// Assert
t.NoError(err)
t.Equal(20, read)
t.Equal([]byte("Hello World!"), bytesRead)
}

func TestExternalTypes(t *testing.T) {
suite.Run(t, new(ExternalTypesTests))
}
174 changes: 174 additions & 0 deletions examples/mocks/reader/reader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e373d9

Please sign in to comment.