Sendgrid-csharp: Make SendGridClient implement an interface for DI and mocking in tests

Created on 20 Feb 2017  路  7Comments  路  Source: sendgrid/sendgrid-csharp

Issue Summary

I would like to be able to mock the SendGridClient object so I can unit test my code without actually making any calls to the SendGrid API. Unfortunately the SendGridClient does not implement any interfaces so I have to instantiate it directly instead of being able to use dependency injection to inject the interface.

The old 6.X.X version of the SendGrid client included an ITransport interface which could be mocked for testing without actually hitting the API.

help wanted community enhancement

Most helpful comment

Actually, this is a different issue. My PR enables the mocking of http responses. The request at hand is for SendGridClient to implement a (new) ISendGridClient interface so that the SendGridClient can be altogether mocked.

All 7 comments

Hello @Tarball,

I'm adding this to our backlog.

In the meantime, we are using StopLight.io's Prism along with SendGrid API's OAI specification to create a mock client.

The instructions for setting this up locally for testing against a mocked version of the SendGrid API is here: https://github.com/sendgrid/sendgrid-csharp/blob/master/CONTRIBUTING.md#testing (this happens automatically on Travis).

With Best Regards,

Elmer

Hi @thinkingserious

I also want to mock SendGridClient.
Can you please provide update on this? I mean when are you planning to apply this change?

Thank you!

Hello @rodchenkov,

This PR fixes this issue: https://github.com/sendgrid/sendgrid-csharp/pull/434

It should be merged soon. Thanks!

With Best Regards,

Elmer

Actually, this is a different issue. My PR enables the mocking of http responses. The request at hand is for SendGridClient to implement a (new) ISendGridClient interface so that the SendGridClient can be altogether mocked.

Thanks for clarifying @ottomatic!

I've added @rodchenkov's vote to this issue in our backlog.

In the mean time @rodchenkov,

You can mock the SendGrid API currently with Prism.

@rodchenkov Please have alook at StrongGrid . It's an alternative .net client for SendGrid'a API which implements an interface for easy mocking.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

loganwasif005 picture loganwasif005  路  3Comments

AndrewTziAnChan picture AndrewTziAnChan  路  3Comments

shoter picture shoter  路  4Comments

thinkingserious picture thinkingserious  路  4Comments

bogacg picture bogacg  路  3Comments