Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created November 16, 2012 20:17
Show Gist options
  • Save jmarnold/4090516 to your computer and use it in GitHub Desktop.
Save jmarnold/4090516 to your computer and use it in GitHub Desktop.
Automated Testing
public class EmailGateway : IEmailGateway
{
private readonly EmailSettings _settings;
public EmailGateway(EmailSettings settings)
{
_settings = settings;
}
public void Send(MailMessage message)
{
message.From = new MailAddress(_settings.From);
using (var client = new SmtpClient(_settings.Host, _settings.Port))
{
client.Send(message);
}
}
}
[FormatAs("Start/Configure the SMTP Server")]
public void StubEmailGateway()
{
var port = _emails.Start();
Retrieve<IContainer>().Inject(new EmailSettings
{
Host = "localhost",
Port = port
});
}
public class RegistrationFixture : ScreenFixture<CreateAccount>
{
public RegistrationFixture()
{
Title = "In the 'Registration' screen";
EditableElementsForAllImmediateProperties();
}
protected override void beforeRunning()
{
Navigation.NavigateTo<RegistrationEndpoint>(x => x.get_register(null));
}
public IGrammar VerifySubscriptionPlans()
{
return VerifySetOf(() => plans())
.Titled("Verify subscription plans")
.MatchOn(x => x.Name);
}
private IEnumerable<SubscriptionPlan> plans()
{
return Driver.InputFor<CreateAccount>(x => x.Subscriptions)
.FindElements(By.TagName("option"))
.Select(x => new SubscriptionPlan {Name = x.Text})
.ToList();
}
[FormatAs("Click the 'Create Account' button")]
public void ClickTheSubmitButton()
{
Driver.FindElement(By.Id(RegistrationEndpoint.Submit)).Click();
}
[FormatAs("Verify that a confirmation email was received")]
public bool VerifyEmail()
{
return Retrieve<SmtpServerController>().RecordedMessages().Any();
}
}
public class SmtpServerController
{
private readonly Lazy<EmbeddedSmtpServer> _server;
private int _port;
public SmtpServerController()
{
_port = 5600;
_server = new Lazy<EmbeddedSmtpServer>(() =>
{
_port = PortFinder.FindPort(_port);
var server = new EmbeddedSmtpServer(_port);
return server;
});
}
public int Start()
{
_server.Value.Start();
return _port;
}
public void Shutdown()
{
if(_server.IsValueCreated)
{
_server.Value.Stop();
}
}
public IEnumerable<MailMessage> RecordedMessages()
{
Shutdown();
return _server.Value.ReceivedMessages();
}
}
public class SystemStateFixture : Fixture
{
public SystemStateFixture()
{
Title = "The system state is";
}
private IEntityRepository _repository;
private SmtpServerController _emails;
public override void SetUp(ITestContext context)
{
_repository = Retrieve<IEntityRepository>();
_emails = new SmtpServerController();
Store(_emails);
}
public override void TearDown()
{
_emails.Shutdown();
}
[FormatAs("Start/Configure the SMTP Server")]
public void StubEmailGateway()
{
var port = _emails.Start();
Retrieve<IContainer>().Inject(new EmailSettings
{
Host = "localhost",
Port = port
});
}
public IGrammar TheSubscriptionPlans()
{
return CreateNewObject<SubscriptionPlan>(x =>
{
x.SetProperty(p => p.Name);
x.Do = plan => _repository.Update(plan);
}).AsTable("The Subscription Plans are").Before(() => _repository.DeleteAll<SubscriptionPlan>());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment