Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

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.

All the questions/articles I have found like this one: Unit testing RavenDb queriesUnit testing RavenDb queries 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.

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)

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 verify what was added to the database in a unit test? Does anyone have any examples of unit tests involving RavenDb in MVC applications?

I'm using MSTest if that matters but I'm happy to try and translate tests from other frameworks.

Thanks.

EDIT

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.

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

What am I missing? How do I get the session created to use in the test?

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.

All the questions/articles I have found like this one: Unit testing RavenDb queries 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.

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)

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 verify what was added to the database in a unit test? Does anyone have any examples of unit tests involving RavenDb in MVC applications?

I'm using MSTest if that matters but I'm happy to try and translate tests from other frameworks.

Thanks.

EDIT

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.

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

What am I missing? How do I get the session created to use in the test?

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.

All the questions/articles I have found like this one: Unit testing RavenDb queries 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.

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)

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 verify what was added to the database in a unit test? Does anyone have any examples of unit tests involving RavenDb in MVC applications?

I'm using MSTest if that matters but I'm happy to try and translate tests from other frameworks.

Thanks.

EDIT

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.

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

What am I missing? How do I get the session created to use in the test?

Added more information
Source Link
Nick
  • 4.2k
  • 10
  • 47
  • 57

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.

All the questions/articles I have found like this one: Unit testing RavenDb queries 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.

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)

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 verify what was added to the database in a unit test? Does anyone have any examples of unit tests involving RavenDb in MVC applications?

I'm using MSTest if that matters but I'm happy to try and translate tests from other frameworks.

Thanks.

EDIT

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.

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

What am I missing? How do I get the session created to use in the test?

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.

All the questions/articles I have found like this one: Unit testing RavenDb queries 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.

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)

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 verify what was added to the database in a unit test? Does anyone have any examples of unit tests involving RavenDb in MVC applications?

I'm using MSTest if that matters but I'm happy to try and translate tests from other frameworks.

Thanks.

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.

All the questions/articles I have found like this one: Unit testing RavenDb queries 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.

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)

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 verify what was added to the database in a unit test? Does anyone have any examples of unit tests involving RavenDb in MVC applications?

I'm using MSTest if that matters but I'm happy to try and translate tests from other frameworks.

Thanks.

EDIT

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.

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

What am I missing? How do I get the session created to use in the test?

Source Link
Nick
  • 4.2k
  • 10
  • 47
  • 57

ASP.NET MVC, RavenDb and Unit Testing

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.

All the questions/articles I have found like this one: Unit testing RavenDb queries 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.

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)

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 verify what was added to the database in a unit test? Does anyone have any examples of unit tests involving RavenDb in MVC applications?

I'm using MSTest if that matters but I'm happy to try and translate tests from other frameworks.

Thanks.