Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create interface for testability #421

Open
amclay opened this issue Oct 29, 2020 · 2 comments
Open

Create interface for testability #421

amclay opened this issue Oct 29, 2020 · 2 comments
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap

Comments

@amclay
Copy link

amclay commented Oct 29, 2020

Issue Summary

It would be nice to have an interface available for mocking or a provided fake client that allows clients to utilize the library without sending real emails.

Steps to Reproduce

N/A

Code Snippet

ex:

type SendGrid interface {
  Send(email *mail.SGMailV3) (*rest.Response, error)
}

Then consumers can create their own mocks using https://github.com/vektra/mockery or https://github.com/maxbrunsfeld/counterfeiter

@childish-sambino
Copy link
Contributor

Relates to #420

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

@childish-sambino childish-sambino added status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap labels Oct 30, 2020
@ncostamagna
Copy link

Hello,
I created a SendGrid interface in #420 for test integration

package main

import (
	"fmt"
	"os"

	"github.com/sendgrid/sendgrid-go"
	"github.com/sendgrid/sendgrid-go/helpers/mail"
	"github.com/sendgrid/sendgrid-go/helpers/mock"
)

func main() {

	// start mocks server
	mock.StartTestServer()

	// add mock value
	mock.Add(&mock.Mock{
		StatusCode: 400,
		Body:       `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`,
	})


	simpleSendMail() // Response with mock data

	// stop mocks server
	mock.StopTestServer()

}

func simpleSendMail() {
	from := mail.NewEmail("Example User", "[email protected]")
	subject := "Sending with Twilio SendGrid is Fun"
	to := mail.NewEmail("Example User", "[email protected]")
	plainTextContent := "and easy to do anywhere, even with Go"
	htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
	message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
	
	// create mock client
	client := sendgrid.NewSendClientMock(os.Getenv("SENDGRID_API_KEY"))

	response, err := client.Send(message)
	if err != nil {
		fmt.Println("Simple Sengird Error: ")
		fmt.Println(err)
		fmt.Println("________________________________")
		fmt.Println()
	} else {
		fmt.Println("Simple Sengird Response: ")
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
		fmt.Println("________________________________")
		fmt.Println()
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

No branches or pull requests

3 participants