Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7435741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:07:32+00:00 2026-05-29T10:07:32+00:00

I want to test my AccountController . The problem is that in Register method

  • 0

I want to test my AccountController. The problem is that in Register method I use the next line to create the user:

Membership.CreateUser(model.Email, model.Password, model.Email, null, null, true, null, out createStatus);

In the web application I use a CustomMembershipProvider which I set using web.config. In my unit test Membership class in standard SqlMembershipProvider. And not my CustomMembershipProvider that I use in my app.

How can I set up membership in unit test context? I mean to set it up programatically as asp net set it after reading web.config file.

I already use interface for mocking users management data layer but I was thinking if there is a way to avoid interface in this case. To be able to set up a mock implementation of membership in unit test.

public void RegisterTest()
{
    IUsersManager repository = new Tests.Data.UsersManager();
    AccountController target = new AccountController(repository); 
    //be able to set Membership underlying provider
    Membership.Provider = new MockMembershipProvider();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:07:33+00:00Added an answer on May 29, 2026 at 10:07 am

    Define a membership interface, something like this:

    public interface IMembershipProvider
    {
        void CreateUser(string username, string password);
    }
    

    …implement it for your MVC application like this:

    public class AspDotNetMembershipProvider : IMembershipProvider
    {
        public void CreateUser(string username, string password)
        {
            string createStatus;
    
            Membership.CreateUser(
                username,
                password,
                username,
                null,
                null,
                true,
                null,
                out createStatus);
    
            // throw an exception if createStatus isn't as expected
        }
    }
    

    …then inject it into your controller and use it like this:

    public class AccountController
    {
        private readonly IMembershipProvider _membershipProvider;
    
        public AccountController(IMembershipProvider membershipProvider)
        {
            this._membershipProvider = membershipProvider;
        }
    
        public ActionResult Register(RegistrationModel model)
        {
            // Try and catch this, returning a success ActionResult if it worked:
            this._membershipProvider.CreateUser(model.Email, model.Password);
        }
    }
    

    ASP.NET uses static classes like Membership for a number of things, but static class access always makes unit testing difficult. The standard solution is to define an interface for the service, implement it using the static ASP.NET class, and inject it into your controllers.

    You can set up the injection (if you’ve not already) by using a default DependencyResolver and a DI container like Unity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to test that in case of some fail no method will be
I want to test a JSF Backing-Bean method isInProgress that delegates to a service
I want to test the web pages I create in all the modern versions
I want to test a piece of code that uses network (the NSURLConnection class,
I want to test a method defined in a rake task. rake file #lib/tasks/simple_task.rake
I want to test the functionality of an activity that contains a ListFragment ,
I want to test in-App purchase before publishing.How do i create a test account
I want to test that setting a certain property (or more generally, executing some
I want to test againts multiple command line arguments in a loop > python
I want to test some methods that call others in the same class. They

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.