ASP.NET MVC, RavenDb, and Unit Testing

I am just starting with RavenDB and I like it. However, I was fixated on how I should unit test control the actions of the controller that interact with it.

All the questions / articles I found here: RavenDb unit tests tell me that I should use RavenDB in memory and not mock it, but I cannot find a convincing example of how this is done.

For example, I have a controller action to add an employee to the database (yes, this is too simplified, but I do not want to complicate the problem)

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 && 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");
  }

How can I check what has been added to the database in unit test? Does anyone have examples of unit tests involving RavenDb in MVC applications?

MSTest, , .

.

, , , , OnActionExecuting , , .

[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.
  }
}

? , ?

+5
3

unit test , .

var newDoc = session.Load<T>(docId)

var docs = session.Query<T>.Where(....).ToList();

RavenDB , , :

  • doc ( )
  • , unit test
  • unit test
  • , .

. , , RacoonBlog, Ayende . 2 :

+7

, unit test?

. . , .

unit test , - (, db), .

EDIT:

, MSDN ( ):

- , , , .

- . unit test - - . , db ? , ?

. -1 , .

+3

, , EmbeddableDocumentStore, RavenDB.

Here's how to install this: http://msdn.microsoft.com/en-us/magazine/hh547101.aspx

Here's how to use the raven repository template so you can easily test: http://novuscraft.com/blog/ravendb-and-the-repository-pattern

+1
source

All Articles