简体   繁体   中英

How do I TDD a custom membership provider and custom membership user?

I need to create a custom membership user and provider for an ASP.NET mvc app and I'm looking to use TDD. I have created a User class which inherits from the MembershipUser class, but when I try to test it I get an error that I can't figure out. How do I give it a valid provider name? Do I just need to add it to web.config? But I'm not even testing the web app at this point.

[failure] UserTests.SetUp.UserShouldHaveMembershipUserProperties TestCase 'UserTests.SetUp.UserShouldHaveMembershipUserProperties' failed: The membership provider name specified is invalid. Parameter name: providerName System.ArgumentException Message: The membership provider name specified is invalid. Parameter name: providerName Source: System.Web

The configuration to add to your unit test project configuration file would look something like this:

  <connectionStrings>
     <remove name="LocalSqlServer"/>
     <add name="LocalSqlServer" connectionString="<connection string>" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
     <membership defaultProvider="provider">
       <providers>
         <add name="provider" applicationName="MyApp" type="System.Web.Security.SqlMembershipProvider" connectionStringName="LocalSqlServer" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" requiresQuestionAndAnswer="false" maxInvalidPasswordAttempts="3" passwordAttemptWindow="15"/>
       </providers>
     </membership>
  </system.web>

Yes, you need to configure it in your configuration file (probably not web.config for a test library, but app.config). You still use the section and within that the section to do the configuration. Once you have that in place, you'll be able to instantiate your user and go about testing it. At which point you'll likely encounter new problems, which you should post as separate questions, I think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM