This commit is contained in:
Mat Ryer 2016-08-29 13:31:32 +01:00
parent 8a4743a1d2
commit 2f4206a8e6

View File

@ -20,7 +20,9 @@ go install github.com/matryer/moq
Mocking interfaces is a nice way to write unit tests where you can easily control the behaviour of the mocked object. Mocking interfaces is a nice way to write unit tests where you can easily control the behaviour of the mocked object.
Moq creates a struct that has a function field for each method, which you can declare in your test code: Moq creates a struct that has a function field for each method, which you can declare in your test code.
This this example, Moq generated the `EmailSenderMock` type:
```go ```go
func TestCompleteSignup(t *testing.T) { func TestCompleteSignup(t *testing.T) {
@ -28,7 +30,7 @@ func TestCompleteSignup(t *testing.T) {
called := false called := false
var sentTo string var sentTo string
mockedEmailSender = &SenderMock{ mockedEmailSender = &EmailSenderMock{
SendFunc: func(to, subject, body string) error { SendFunc: func(to, subject, body string) error {
called = true called = true
sentTo = to sentTo = to
@ -46,6 +48,10 @@ func TestCompleteSignup(t *testing.T) {
} }
} }
func CompleteSignUp(to string, sender EmailSender) {
// TODO: this
}
``` ```
The mocked structure implements the interface, where each method calls the associated function field. The mocked structure implements the interface, where each method calls the associated function field.