Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I "uninitialize" WebSecurity when unit testing?
    primarykey
    data
    text
    <h2>My situation</h2> <p>I'm putting tests in an ASP.NET MVC4 application. The part of the app I'm developing combines modern WebSecurity/SimpleMembershipProvider with a legacy authentication system that's being phased out. <del>I'd love to do proper unit tests on this, but for practical reasons I'm choosing not to mock or stub out WebSecurity; I'm just pointing it at a test database.</del>*</p> <h2>The problem:</h2> <p>Using NUnit, I want to set up WebSecurity for a series of tests, using the <code>WebSecurity.InitializeDatabaseConnection()</code> method. This method is designed to only run once, preferably at app startup.</p> <p>I'd like to isolate my tests, wiping the database and calling <code>InitializeDatabaseConnection</code> before each test (i.e. in a <code>[Setup]</code> method). However, WebSecurity maintains its Initialized state statically, which means it throws an exception if it is run more than once in the same app instance.</p> <p>In other words, only my first test works; subsequent ones throw exceptions:</p> <pre><code>System.InvalidOperationException : The "WebSecurity.InitializeDatabaseConnection" method can be called only once. </code></pre> <h2>The question:</h2> <p><strong>Is there any way I can "uninitialize" WebSecurity so that I can initialize it more than once in a single app instance? Alternately, is there a way to indicate that NUnit should restart the app before each test in a certain fixture?</strong></p> <h2>Notes:</h2> <p>I've tried just calling <code>WebSecurity.InitializeDatabaseConnection()</code> once in a <code>[TestFixtureSetup]</code> method or a <code>[SetUpFixture]</code> and just wiping the relevant tables between tests, but I'd really prefer to have a guaranteed clean state. Additionally, this approach has an unfortunate side effect: if I want to use the NUnit GUI to run a few tests in sequence, then a few more in sequence, I must reload the assembly between sequences. The GUI runs these setup methods once per test sequence, but apparently maintains the same app instance until the assembly is reloaded.</p> <hr> <p><sub>* I decided to mock WebSecurity after all; see my self-answer below.</sub></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload