Moq creates a struct where each interface method is represented by a function field. In your tests, you assign a function to these fields to define the mock's behavior. You can use captured variables from the test scope to verify interactions.
func TestCompleteSignup(t *testing.T) {
var sentTo string
// Initialize the mock with custom behavior
mockedEmailSender = &EmailSenderMock{
SendFunc: func(to, subject, body string) error {
sentTo = to
return nil
},
}
CompleteSignUp("me@email.com", mockedEmailSender)
// Verify behavior using call tracking
callsToSend := len(mockedEmailSender.SendCalls())
if callsToSend != 1 {
t.Errorf("Send was called %d times", callsToSend)
}
if sentTo != "me@email.com" {
t.Errorf("unexpected recipient: %s", sentTo)
}
}