unit-testing - 南希FX : Can I force my unit-test browser to be authenticated by default?

标签 unit-testing nancy

这是一个单元测试,显示我对我的 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/

相关文章:

unit-testing - Jest 模拟函数调用不计算在内

java - 在测试期间注入(inject)@Autowired 私有(private)字段

unit-testing - 忽略 Golang 测试覆盖率计算中的代码块

c# - 南希开始接受请求很慢

c# - 服务器向 Nancy 发送事件

javascript - 在单元测试中断言流类型错误

java - 我如何在 2.7.x 中使用 Mockito.doReturn

.net - 我可以告诉 Nancy 仅在当前程序集中搜索模块吗?

c# - 过滤 json 字符串中的无效值

Nancy 框架示例应用程序