views:

640

answers:

4

I’m not sure how to mock an ASP.NET Membership for my controller test.

Controller Code:

MembershipUser username = Membership.GetUser();
string UserID = username.UserName.ToString();

Does anyone know how to mock this for a controller test? I'm using RhinoMocks.

+3  A: 

I would watch the MVS StoreFront Serieshttp://www.asp.net/learn/mvc-videos/

For one on Mocking -

http://www.asp.net/learn/mvc-videos/video-365.aspx

And the Membership one http://www.asp.net/learn/mvc-videos/video-372.aspx

One for Membership and the view of refactor with OpenID

http://www.asp.net/learn/mvc-videos/video-425.aspx

A: 

i am also facing the same issue?

if someone has any solutions please share..

prakash
Could you delete this and add as a comment to the original question?
Richard Ev
+1  A: 

I've started working on something like this. Rather than doing a true mock, I created a FakeMembershipProvider that just implements the minimum of MembershipProvider that I need and provides a way to set the users and such. I'm doing the same for RoleProvider. Then I've set the App.config for my test project so it uses these as the providers.

So far, it seems to be working well.

Michael Johnson
A: 

To mock the objects connectected with Membership static class, you should use its Abstract classes in that case, for mocking the GetUser() method, use MembershipProvider class, it is possible to mock, just pass it to your controller and it's done.

Good luck, if you'll have any problems, just let me now, I'll post up some code examples.

Sergej Kravcenko