Skip to content

Instantly share code, notes, and snippets.

@vandatech
Created April 11, 2013 08:07
Show Gist options
  • Save vandatech/5361608 to your computer and use it in GitHub Desktop.
Save vandatech/5361608 to your computer and use it in GitHub Desktop.
Cant test handling of AWS exceptions because constructors are internal ?
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