4

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

非常感谢您的帮助,

4

2 回答 2

9

Since you are using mvc, if you create a new project and look in the Account controller's code you will find the following clarification:

The FormsAuthentication type is sealed and contains static members, so it is difficult to unit test code that calls its members. The interface and helper class below demonstrate how to create an abstract wrapper around such a type in order to make the AccountController code unit testable.

Below this, there are these declarations:

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

You may look for implementations in the referred code.

于 2009-04-12T22:23:14.253 回答
3

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

于 2009-04-12T22:09:19.420 回答