Reputation: 9242
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.
Upvotes: 0
Views: 1692
Reputation: 14850
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
Upvotes: 4