Skip to content

Instantly share code, notes, and snippets.

@MattHoneycutt
Created November 27, 2011 18:43
Show Gist options
  • Save MattHoneycutt/1397956 to your computer and use it in GitHub Desktop.
Save MattHoneycutt/1397956 to your computer and use it in GitHub Desktop.
SpecsFor.Mvc examples
[SetUpFixture]
public class DemoWebAppConfig : SpecsForMvcConfig
{
public DemoWebAppConfig()
{
UseIISExpressWith(Project("SpecsFor.Mvc.Demo"));
BuildRoutesUsing(r => MvcApplication.RegisterRoutes(r));
UseBrowser(BrowserDriver.InternetExplorer);
//TODO: Open questions to be answered:
//1) How do we point the app at a database for testing?
}
[SetUp]
public override void SetupTestRun()
{
base.SetupTestRun();
}
[TearDown]
public override void TearDownTestRun()
{
base.TearDownTestRun();
}
}
public class LoginSpecs
{
public class when_logging_in_with_an_invalid_username_and_password : SpecsFor<MvcWebApp>
{
protected override void Given()
{
SUT.NavigateTo<AccountController>(c => c.LogOn());
}
protected override void When()
{
SUT.FindFormFor<LogOnModel>()
//TODO: Eventually expose a fluent API like:
//.ForField(m => m.UserName).TypeText("bad@user.com")
.SetFieldValue(m => m.UserName, "bad@user.com")
.SetFieldValue(m => m.Password, "BadPass")
.Submit();
}
[Test]
public void then_it_should_redisplay_the_page()
{
SUT.Route.ShouldMapTo<AccountController>(c => c.LogOn());
}
[Test]
public void then_it_should_contain_a_validation_error()
{
SUT.ValidationSummary.Text.ShouldContain("The user name or password provided is incorrect.");
}
}
public class when_logging_in_with_valid_credentials : SpecsFor<MvcWebApp>
{
protected override void Given()
{
SUT.NavigateTo<AccountController>(c => c.LogOn());
}
protected override void When()
{
SUT.FindFormFor<LogOnModel>()
.SetFieldValue(m => m.UserName, "real@user.com")
.SetFieldValue(m => m.Password, "RealPassword")
.Submit();
}
[Test]
public void then_it_redirects_to_the_home_page()
{
SUT.Route.ShouldMapTo<HomeController>(c => c.Index());
}
}
}
[TestFixture]
public class LoginSpecsWithoutSpecsForMvc
{
[Test]
public void when_logging_in_with_an_invalid_username_and_password()
{
var capabilities = new DesiredCapabilities();
capabilities.SetCapability(InternetExplorerDriver.IntroduceInstabilityByIgnoringProtectedModeSettings, true);
var driver = new InternetExplorerDriver(capabilities);
try
{
driver.Navigate().GoToUrl("http://localhost:52125/Account/LogOn");
driver.FindElement(By.Name("UserName")).SendKeys("bad@user.com");
driver.FindElement(By.Name("Password")).SendKeys("BadPass");
driver.FindElement(By.TagName("form")).Submit();
driver.Url.ShouldEqual("http://localhost:52125/Account/LogOn");
driver.FindElement(By.ClassName("validation-summary-errors")).Text.ShouldContain(
"The user name or password provided is incorrect.");
}
finally
{
driver.Close();
}
}
[Test]
public void when_logging_in_with_a_valid_username_and_password()
{
var capabilities = new DesiredCapabilities();
capabilities.SetCapability(InternetExplorerDriver.IntroduceInstabilityByIgnoringProtectedModeSettings, true);
var driver = new InternetExplorerDriver(capabilities);
try
{
driver.Navigate().GoToUrl("http://localhost:52125/Account/LogOn");
driver.FindElement(By.Name("UserName")).SendKeys("real@user.com");
driver.FindElement(By.Name("Password")).SendKeys("RealPassword");
driver.FindElement(By.TagName("form")).Submit();
driver.Url.ShouldEqual("http://localhost:52125/");
}
finally
{
driver.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment