3
我想單元測試一個ASP.NET MVC 4控制器返回Kendo.UI.Mvc DataSource結果包裝爲Json。問題是我無法到達返回的實際數據..它始終爲空。單元測試與Kendo的ASP.NET MVC4控制器
問題是,我如何驗證從包裝Kendo.UI DataSourceResult的控制器返回的JSON。
我想單元測試一個ASP.NET MVC 4控制器返回Kendo.UI.Mvc DataSource結果包裝爲Json。問題是我無法到達返回的實際數據..它始終爲空。單元測試與Kendo的ASP.NET MVC4控制器
問題是,我如何驗證從包裝Kendo.UI DataSourceResult的控制器返回的JSON。
這個問題令人生氣,因爲我可以在調試時看到VS想要的數據集合。我更新了測試夾具 - 在模型數據上可以執行斷言。
基本上我做了以下內容:
控制器:
public ActionResult EditRead([DataSourceRequest] DataSourceRequest request)
{
return Json(GetViewModel().ToDataSourceResult(request));
}
單元測試:
[Test]
public void EditRead_Should_Read_List_Or_Pharmacies()
{
//Create test db
var db = new FakePharmacyDirectoryDb();
db.AddSet(TestData.PharmacyLocations(10));
//setup controller, we need to mock a DataSourceRequest
//that Kendo.Mvc uses to communicate with the View
var controller = new DirectoryController(db);
var kendoDataRequest = new DataSourceRequest();
//get the result back from the controller
var controllerResult = controller.EditRead(kendoDataRequest);
//cast the results to Json
var jsonResult = controllerResult as JsonResult;
//at runtime, jsonRsult.Data data will return variable of type Kendo.Mvc.UI.DataSourceResult
dynamic kendoResultData = jsonResult.Data;
//... which you can then cast DataSourceResult.Data as
//the return type you are trying to test
var results = kendoResultData.Data as List<PharmacyLocation>;
Assert.IsInstanceOf<List<PharmacyLocation>>(results);
Assert.AreEqual(10,results.Count);
}