我刚刚开始学习和使用 ASP.NET MVC 2,并且更多地参与了我的代码的单元测试。我的问题大致是如何通过在我的测试中传递凭据来模拟用户登录。
我正在使用 MSpec 并试图了解 fakeiteasy 以编写我的测试。到目前为止,我相信当未经身份验证的用户尝试访问页面时,我已经正确编写了一个测试(它通过了测试条件)。
Subject( typeof( HomeController ) )]
public class context_for_a_home_controller_for_not_logged_user
{
protected static HomeController HomeController;
Establish context = () =>
{
// Create controller
HomeController = new HomeController();
HomeController.ControllerContext = A.Fake<ControllerContext>();
};
}
[Subject(typeof(HomeController))]
public class when_the_home_page_is_requested : context_for_a_home_controller_for_not_logged_user
{
static ActionResult result;
Because of = () =>
result = HomeController.Index();
It should_return_the_log_in_page_if_user_not_logged_in = () =>
{ result.ShouldBeAView().And().ShouldUseDefaultView(); };
}
到目前为止,一切都很好。但是,我想测试经过身份验证的用户何时点击家庭控制器的场景。我被困在如何模拟经过身份验证的用户上,欢迎提供任何帮助或建议。
TIA,
大卫