berlaga
berlaga

Reputation: 11

Unit testing AccountController from default ASP.Net MVC 4 Application project

I'm working on MVC4 application and interested in creating a set of controller unit tests. All my attempts to unit test methods from AccountController end up with the same exception related to WebMatrix.WebData.WebSecurity:

To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".

I'm using SimpleMemebership provider that is being initialized through InitializeSimpleMembershipAttribute

Can someone provide guidance how to do this properly?

Upvotes: 1

Views: 1397

Answers (1)

bas
bas

Reputation: 14982

I'll drop it as answer since this will become painfully ugly in a comment. What I did, is add a membershipprovider in my test project. I have a custom membership provider, but in your case the app.config would look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <clear/>
        <add name="SimpleMembershipProvider"
             type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/>
      </providers>
    </membership>
  </system.web>
</configuration>

You probably get complaints about a missing reference of WebMatrix. Add the following references:

  • WebMatrix.Data
  • WebMatrix.WebData

Set copy to local to true.

Then make sure you call the WebSecurity.InitializeDatabaseConnection and you're good to go.

Hope this helps.

Upvotes: 1

Related Questions