title: How do you mock ServiceStack ISession using Moq and StructureMap?
body: <p>I'm using ServiceStack / StructureMap / Moq.  The service makes a call to Session, which is type ServiceStack.CacheAccess.ISession.  For unit tests, I created a Mock object using Moq, and added it to the StructureMap configuration:</p>
  
  <pre><code>protected Mock&lt;ISession&gt; sessionMock = new Mock&lt;ISession&gt;();
  ObjectFactory.Configure(
      cfg =&gt;
         {
             cfg.For&lt;ISession&gt;().Use(sessionMock.Object);
  </code></pre>
  
  <p>However, I was not surprised when the Session object was null -- I'm pretty sure I'm leaving out a step.  What else do I need to do to fill my Session property with a mock object?</p>
  
  <p>[EDIT] Here's a simple test scenario</p>
  
  <p>Code to test.  Simple request / service</p>
  
  <pre><code>[Route("getKey/{key}")]
  public class MyRequest:IReturn&lt;string&gt;
  {
      public string Key { get; set; }
  }
  
  public class MyService:Service
  {
      public string Get(MyRequest request)
      {
          return (string) Session[request.Key];
      }
  }
  </code></pre>
  
  <p>The base test class and MockSession classes</p>
  
  <pre><code>// test base class
  public abstract class MyTestBase : TestBase
  {
      protected IRestClient Client { get; set; }
  
      protected override void Configure(Container container)
      {
          // this code is never reached under any of my scenarios below
          container.Adapter = new StructureMapContainerAdapter();
          ObjectFactory.Initialize(
              cfg =&gt;
                  {
                      cfg.For&lt;ISession&gt;().Singleton().Use&lt;MockSession&gt;();
                  });
      }
  }
  
  public class MockSession : ISession
  {
      private Dictionary&lt;string, object&gt; m_SessionStorage = new Dictionary&lt;string, object&gt;();
  
      public void Set&lt;T&gt;(string key, T value)
      {
          m_SessionStorage[key] = value;
      }
  
      public T Get&lt;T&gt;(string key)
      {
          return (T)m_SessionStorage[key];
      }
  
      public object this[string key]
      {
          get { return m_SessionStorage[key]; }
          set { m_SessionStorage[key] = value; }
      }
  }
  </code></pre>
  
  <p>And tests. See comments for where I'm seeing the failure.  I didn't really expect versions 1 &amp; 2 to work, but hoped version 3 would.</p>
  
  <pre><code>[TestFixture]
  public class When_getting_a_session_value:MyTestBase
  {
      [Test]
      public void Test_version_1()
      {
          var session = ObjectFactory.GetInstance&lt;MockSession&gt;();
          session["key1"] = "Test";
          var request = new MyRequest {Key = "key1"};
          var client = new MyService();  // generally works fine, except for things like Session
          var result = client.Get(request);  // throws NRE inside MyService
          result.ShouldEqual("Test");
      }
  
      [Test]
      public void Test_version_2()
      {
          var session = ObjectFactory.GetInstance&lt;MockSession&gt;();
          session["key1"] = "Test";
          var request = new MyRequest {Key = "key1"};
          var client = ObjectFactory.GetInstance&lt;MyService&gt;();
          var result = client.Get(request);  // throws NRE inside MyService
          result.ShouldEqual("Test");
      }
      [Test]
      public void Test_version_3()
      {
          var session = ObjectFactory.GetInstance&lt;MockSession&gt;();
          session["key1"] = "Test";
          var request = new MyRequest {Key = "key1"};
          var client = CreateNewRestClient();
          var result = client.Get(request);  // throws NotImplementedException here
          result.ShouldEqual("Test");
      }
  }
  </code></pre>