0

I have some appsetting related to SendGrid Service like username, password and some resetpasswordemail settings. Now i wanted to use them in my unit test. My ResetPasswordSetting class looks like this.

public class ResetPasswordEmailSetting : IResetPasswordEmailSetting
{
    public string ResetPasswordEmailUrl { get; set; }
    public List<string> AddToEmails { get; set; }
    public string FromEmail
    {
        get { return ConfigurationManager.AppSettings.Get("ResetPassword_FromEmail"); }
    }
    public string Subject
    {
        get { return ConfigurationManager.AppSettings.Get("ResetPassword_Subject"); }

    }
    public string SenderDisplayName
    {
        get { return ConfigurationManager.AppSettings.Get("ResetPassword_SenderDisplayName"); }
    }
}

When i run my unit test using this class then the fields that get their value from Webconfig appsetting part are coming null.

My Unit Test looks like this.

[TestMethod]
[TestCategory("SendGrid-Service")]
public async Task email_recieved_when_correct_email_provided()
{
    var sendgrid = new SendGridService();

    var resetpasswordsetting = new ResetPasswordEmailSetting
    {
        AddToEmails = new List<string> { "[email protected]" },
        ResetPasswordEmailUrl = "www.google.com",
    };   

// when i debug then resetpasswordsetting fields are coming null other then above.                  
 var result = await sendgrid.SendResetPasswordEmail(resetpasswordsetting, new    
               SendGridSettings());

        result.Should().BeTrue();
 }

so any idea how to get these settings in unit test.

1 Answer 1

4

The first thing that comes to my mind is that in this test you are NOT interested in the settings, the subject of your test is SendResetPasswordEmail method from the SendGridService class. Remember you are implementing UNIT tests, not integration tests or something like that. While unit testing you should strive to isolate any dependencies and config settings is one dependency your class doesn't really need to test a specific business logic. For this purpose you should create a mock object instead because, again, config settings are NOT needed to test a piece of functionality.

However, if you still insist in using real config files there's nothing stopping you from adding an app.config file to your unit test project and specifying all the appSettings your tests require

Sign up to request clarification or add additional context in comments.

2 Comments

You mean to say i have to mock the "ResetPasswordEmailSetting" , and "SendGridSettings".
Well, it's not that you have to because it'll work either way. But if you want to really unit test then you should mock them since you're only interested in SendResetPasswordEmail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.