Ros
Ros

Reputation: 634

ASP.NET MVC – Mock Membership for Controller Test

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.

Upvotes: 5

Views: 2496

Answers (3)

Sergey Kravchenko
Sergey Kravchenko

Reputation: 182

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.

Upvotes: -1

Michael Johnson
Michael Johnson

Reputation: 2307

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.

Upvotes: 5

littlegeek
littlegeek

Reputation:

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

Upvotes: 3

Related Questions