Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>look at the <code>HttpSessionStateBase</code> and <code>HttpSessionStateWrapper</code> classes in <code>System.Web.Abstractions</code>. <code>HttpSessionStateBase</code> is the abstract class from which <code>HttpSessionState</code> inherits, and <code>HttpSessionStateWrapper</code> is used to wrap a sealed class in an abstract class, which you can then mock in your tests.</p> <p>A lot of the <code>System.Web</code> classes are sealed (for example, <code>HttpSessionState</code>), so it's a real pain to test your code when you have methods and classes that interact with them. One pattern I like to use to get around this looks like the following:</p> <pre><code>public void DoSomething(HttpSessionState state) { // take this HttpSeassionState and create an abstract HttpSessionStateBase // instance DoSomething(new HttpSessionStateWrapper(state)); } internal void DoSomething(HttpSessionStateBase state) { // my actual logic for working with the session state } </code></pre> <p>The public method is difficult to test, because <code>HttpSessionState</code> is sealed, and you can't mock it. However, the internal method operates on an <code>HttpSessionStateBase</code> instance, which you <em>can</em> mock. Note that I've marked it as internal because I don't want the outside world to be able to access that method. However, I <em>do</em> want my tests to be able to access that, so I'll modify my AssemblyInfo.cs to include something like this:</p> <pre><code>[assembly: InternalsVisibleTo("Vendor.Utilities.Tests")] </code></pre> <p>Finally, my test for this would look something like this:</p> <pre><code>[Test] public void Test_DoSomething() { HttpSessionStateBase state = MockRepository.PartialMock&lt;HttpSessionStateBase&gt;(); state.Expect(s =&gt; ...); MyClass.DoSomething(state); state.VerifyAllExpectations(); } </code></pre> <p>Hope that helps. Good luck!</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.
    1. VO
      singulars
      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