模拟FormsAuthentication.Authenticate()方法

时间:2009-04-12 21:10:58

标签: asp.net-mvc forms-authentication rhino-mocks

有什么方法可以用测试凭证模拟FormsAuthentication.Authenticate(“username”,“password”)方法?我的测试目标是确保如果身份验证失败,它会重定向到正确的位置。我正在使用Rhino Mocks作为模拟框架。

非常感谢你的帮助,

2 个答案:

答案 0 :(得分:9)

由于您使用的是mvc,如果您创建一个新项目并查看帐户控制器的代码,您将找到以下说明:

  

FormsAuthentication类型已被密封   并包含静态成员,所以它是   难以单元测试调用的代码   其成员。界面和帮助器   下面的课程演示如何创建   这种类型的抽象包装器   为了使AccountController   代码单元可测试。

在此之下,有以下声明:

public interface IFormsAuthentication
public class FormsAuthenticationService : IFormsAuthentication
public interface IMembershipService
public class AccountMembershipService : IMembershipService

您可以在引用的代码中查找实现。

答案 1 :(得分:3)

我不使用Rhino Mocks,但我认为除了typemock之外,不可能使用任何.Net模拟框架来模拟静态方法。最好的解决方法是在它周围创建一个接口适配器。