title: ASP.NET MVC, RavenDb and Unit Testing
body: <p>I'm just getting started with RavenDB and I like it so far.  I am however stuck on how I should unit test controller actions that interact with it.</p>
  
  <p>All the questions/articles I have found like this one: <a href="https://stackoverflow.com/questions/8368674/unit-testing-ravendb-queries">Unit testing RavenDb queries</a> tell me I should use RavenDB in memory rather than mock it away but I cannot find a solid example of how this is done.</p>
  
  <p>For example I have a controller action to add an employee to the database (yes, it's overly simplified but I don't want to complicate the issue)</p>
  
  <pre><code>public class EmployeesController : Controller
  {
  
    IDocumentStore _documentStore;
    private IDocumentSession _session;
  
    public EmployeesController(IDocumentStore documentStore)
    {
      this._documentStore = documentStore;
  
    }
  
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      _session = _documentStore.OpenSession("StaffDirectory");
    }
  
    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (_session != null &amp;&amp; filterContext.Exception == null) {
          _session.SaveChanges();
          _session.Dispose();
      }
    }
  
    [HttpGet]
    public ViewResult Create()
    {
      return View();
    }
  
    [HttpPost]
    public RedirectToRouteResult Create(Employee emp)
    {
      ValidateModel(emp);
      _session.Store(emp);
      return RedirectToAction("Index");
    }
  </code></pre>
  
  <p>How can I verify what was added to the database in a unit test?  Does anyone have any examples of unit tests involving RavenDb in MVC applications?</p>
  
  <p>I'm using MSTest if that matters but I'm happy to try and translate tests from other frameworks.</p>
  
  <p>Thanks.</p>
  
  <p>EDIT</p>
  
  <p>Ok, my test initialise creates the document store that is injected into the controller constructor, but when I run my test the OnActionExecuting event doesn't run so there is no session to use and the test fails with a null reference exception.</p>
  
  <pre><code>[TestClass]
  public class EmployeesControllerTests
  {
    IDocumentStore _store;
  
    [TestInitialize]
    public void InitialiseTest()
    {
      _store = new EmbeddableDocumentStore
      {
        RunInMemory = true
      };
      _store.Initialize();
    }
  
    [TestMethod]
    public void CreateInsertsANewEmployeeIntoTheDocumentStore()
    {
      Employee newEmp = new Employee() { FirstName = "Test", Surname = "User" };
  
      var target = new EmployeesController(_store);
      ControllerUtilities.SetUpControllerContext(target, "testUser", "Test User", null);
  
      RedirectToRouteResult actual = target.Create(newEmp);
      Assert.AreEqual("Index", actual.RouteName);
  
      // verify employee was successfully added to the database.
    }
  }
  </code></pre>
  
  <p>What am I missing? How do I get the session created to use in the test?</p>