-
Notifications
You must be signed in to change notification settings - Fork 260
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
feat: create mocks helper #420
Conversation
I love the idea, but I think it'd be more flexible to create an interface, and allow others to mock it in their own apps. |
Ditto on @amclay. The idea of a built in mock is great for getting unit tests, but if this could also include interfacing out the |
@@ -8,6 +8,7 @@ import ( | |||
|
|||
"github.com/sendgrid/rest" | |||
"github.com/sendgrid/sendgrid-go/helpers/mail" | |||
"github.com/sendgrid/sendgrid-go/helpers/mock" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like adding this dependency in non-dev environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @thinkingserious, how are you?
Yes, I think the same but is only place where we could intercept the request
I haven't found other place where we could intercept the request with the Mocks.
Where we could intercept the request with the mocks?
Thank you very much and greetings!
Nahuel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good, thanks for asking. I trust the same is true with you!
I think we should first create the interface per issue #421.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @thinkingserious
I created a SendGrid interface for test integration,
this example is in examples/mocks
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()
}
}
base_interface.go
Outdated
@@ -70,6 +71,10 @@ func API(request rest.Request) (*rest.Response, error) { | |||
|
|||
// MakeRequest attempts a Twilio SendGrid request synchronously. | |||
func MakeRequest(request rest.Request) (*rest.Response, error) { | |||
if mock.Get() != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocking should happen within the test environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hello @thinkingserious
Greate, I think that would be better we use "startTest()" and "endTest()" for setting to begine and end test,
In this way, the developers doesn't depends to the name of environment variable for them project.
best regards
Nahuel
Hello @ncostamagna, I also agree with the comments above and the related issue #421. What are your thoughts? With best regards, Elmer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will need an example of how to get started here.
examples/mocks/mocks.go
Outdated
} | ||
|
||
func sendMail() { | ||
m := mail.NewV3Mail() | ||
content := mail.NewContent("text/html", "<h1>Hello world</h1>This is an example") | ||
|
||
from := mail.NewEmail("envalidMail", "invalidMail@mail.com") | ||
from := mail.NewEmail("Nahuel", "ncostamagna@hotmail.com.ar") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert to the example name and email.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @thinkingserious,
the example of how to get started mocks is in line 19 for start and line 42 for end
mock.StartTestServer() // start mock server
mock.StopTestServer() // end mock server
Do I write documentation in a markdown file?
Thanks you!
Merry christmas and happy new year!
Closing until PR feedback and failing tests are addressed. |
Fixes
create mocks helper features
used for integration test in the applications without need to send any mails
with a complete coverage
example in
Checklist
If you have questions, please file a support ticket, or create a GitHub Issue in this repository.