c# - NUnit test for Void function (Sending Email) -
i have void function, sends email. need write tests function. how can done?
public void sendadminmail(string subject, string body, string adminaddress) { var email = email. from(configurationmanager.appsettings["mail.noreply.address"].tostring(cultureinfo.invariantculture)). to(adminaddress). subject(subject). body(body). usingclient(getofficeclient()); email.message.subjectencoding = encoding.utf8; email.message.bodyencoding = encoding.utf8; email.send(); }
in context hard - unless you'll able switch email
somehow through reflection (as interestingly pointed out juhan_h in answer, maybe not hard nowadays ;) ).
typical solution provide interface class, example interface emailfactory
. you'd have:
private emailfactory emailfactory; public void sendadminmail(string subject, string body, string adminaddress) { var email = emailfactory .from(configurationmanager .appsettings["mail.noreply.address"] .tostring(cultureinfo.invariantculture)) .to(adminaddress) .subject(subject) .body(body) .usingclient(getofficeclient()); email.message.subjectencoding = encoding.utf8; email.message.bodyencoding = encoding.utf8; email.send(); }
and provide stub of factory class, create email mocks on verify correct behavior.
Comments
Post a Comment