-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add example of mocking external types
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
1 parent
19e80b1
commit 3e373d9
Showing
3 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.