views:

486

answers:

2

Is there any way I can mock FormsAuthentication.Authenticate("username", "password") method with test credential? My test goal is to make sure that if authentication fails, it redirects to correct place. I'm using Rhino Mocks as mocking framework.

Thank you very much for your help,

+3  A: 

I don't use Rhino Mocks, but I think it is not possible to mock static methods with any .Net mocking framework except typemock. The best work around is to create an interfaced adapter around it.

Paco
+4  A: 

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.

eKek0