Created
April 11, 2013 08:07
-
-
Save vandatech/5361608 to your computer and use it in GitHub Desktop.
Cant test handling of AWS exceptions because constructors are internal ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
namespace MyLib.Tests | |
{ | |
public class AwsExceptionFactory<T> | |
{ | |
// amazon exceptions have internal constructors and so have to use reflection to create instances | |
public static T GetException() | |
{ | |
return (T)Activator.CreateInstance(typeof(T), | |
BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { "Mock Exception Message" }, null); | |
} | |
} | |
} | |
//This can then be used thus (uses NSubstitue for mocks) : | |
[Test] | |
public void IfServiceExceptionIsThrownDoNotUpdateMailStatusToSent() | |
{ | |
var client = Substitute.For<AmazonSimpleEmailService>(); | |
var mailrepo = Substitute.For<IMailRepository>(); | |
var config = Substitute.For<IConfiguration>(); | |
client.SendEmail(Arg.Any<SendEmailRequest>()).Returns((x => { throw AwsExceptionFactory<AmazonSimpleEmailServiceException>.GetException(); })); | |
var sesMailSender = new SesMailSender(config, client, mailrepo); | |
sesMailSender.SendMail(new Mail()); | |
mailrepo.DidNotReceive().UpdateMailStatus(Arg.Any<Mail>(), Arg.Any<SendStatus>()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment