Created
November 16, 2012 20:17
-
-
Save jmarnold/4090516 to your computer and use it in GitHub Desktop.
Automated Testing
This file contains hidden or 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
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); | |
} | |
} | |
} |
This file contains hidden or 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
[FormatAs("Start/Configure the SMTP Server")] | |
public void StubEmailGateway() | |
{ | |
var port = _emails.Start(); | |
Retrieve<IContainer>().Inject(new EmailSettings | |
{ | |
Host = "localhost", | |
Port = port | |
}); | |
} |
This file contains hidden or 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
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(); | |
} | |
} |
This file contains hidden or 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
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(); | |
} | |
} |
This file contains hidden or 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
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