RavenDBを使い始めたばかりで、今のところ気に入っています。しかし、私はそれと相互作用するコントローラーアクションをユニットテストする方法に固執しています。
私が見つけたすべての質問/記事は次のようになっています。RavenDbクエリのユニットテストでは、RavenDBをモックするのではなく、メモリ内で使用する必要があると言われていますが、これがどのように行われるかについての確かな例は見つかりません。
たとえば、データベースに従業員を追加するコントローラーアクションがあります(はい、それは過度に単純化されていますが、問題を複雑にしたくありません)
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");
}
単体テストでデータベースに何が追加されたかを確認するにはどうすればよいですか?MVCアプリケーションでRavenDbを使用する単体テストの例はありますか?
それが重要な場合はMSTestを使用していますが、他のフレームワークからテストを変換してみてうれしいです。
ありがとう。
編集
テストの初期化により、コントローラーコンストラクターに挿入されるドキュメントストアが作成されますが、テストを実行するとOnActionExecutingイベントが実行されないため、使用するセッションがなく、null参照例外でテストが失敗します。
[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.
}
}
私は何が欠けていますか?テストで使用するためにセッションを作成するにはどうすればよいですか?