这是一个单元测试,显示我对我的 Nancy 浏览器进行身份验证(其他代码已被删除)。我想知道是否有更聪明、更干燥的方法来做到这一点?
[Fact]
public void Login__Should_redirect_from_login_to_requested_page_if_credentials_are_correct()
{
var browser = Fake.Browser();
var response = browser.Post("/login", with =>
{
with.HttpRequest();
with.FormValue("UserName", userName);
with.FormValue("Password", password);
});
response.ShouldHaveRedirectedTo("/");
}
最佳答案
看起来您有一个返回浏览器实例的方法:Fake.Browser()
所以如果需要,为什么不重写它以提供经过身份验证的版本。也许是这样的:
public static Browser Browser(string username = null, string password = null)
{
var browser = new Browser(new UnitTestBootstrapper());
if (username.IsEmpty() || password.IsEmpty()) return browser;
return browser.Post("/login", with =>
{
with.HttpRequest();
with.FormValue("Username", username);
with.FormValue("Password", password);
}).Then;
}
关于unit-testing - 南希FX : Can I force my unit-test browser to be authenticated by default?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10497086/