added tip

This commit is contained in:
Mat Ryer 2017-07-07 14:49:48 +01:00
parent bc529fe78c
commit 09e12df472

View File

@ -63,12 +63,10 @@ This this example, Moq generated the `EmailSenderMock` type:
```go ```go
func TestCompleteSignup(t *testing.T) { func TestCompleteSignup(t *testing.T) {
called := false
var sentTo string var sentTo string
mockedEmailSender = &EmailSenderMock{ mockedEmailSender = &EmailSenderMock{
SendFunc: func(to, subject, body string) error { SendFunc: func(to, subject, body string) error {
called = true
sentTo = to sentTo = to
return nil return nil
}, },
@ -76,8 +74,8 @@ func TestCompleteSignup(t *testing.T) {
CompleteSignUp("me@email.com", mockedEmailSender) CompleteSignUp("me@email.com", mockedEmailSender)
if called == false { if mockedEmailSender.CallsTo.Send != 1 {
t.Error("Sender.Send expected") t.Errorf("Send was called %d times", mockedEmailSender.CallsTo.Send)
} }
if sentTo != "me@email.com" { if sentTo != "me@email.com" {
t.Errorf("unexpected recipient: %s", sentTo) t.Errorf("unexpected recipient: %s", sentTo)
@ -95,8 +93,10 @@ The mocked structure implements the interface, where each method calls the assoc
## Tips ## Tips
* Keep mocked logic inside the test that is using it * Keep mocked logic inside the test that is using it
* Only mock the fields you need - it will panic if a nil function gets called * Only mock the fields you need
* It will panic if a nil function gets called
* Use closured variables inside your test function to capture details about the calls to the methods * Use closured variables inside your test function to capture details about the calls to the methods
* Use `.CallsTo.Method` to track the calls
* Use `go:generate` to invoke the `moq` command * Use `go:generate` to invoke the `moq` command
## License ## License