Tuesday, August 23, 2016

Unit Testing Kendo UI

If you want to Unit Test Kendo UI, unfortunately, there is no documentation on how to accomplish this on the Telerik website.

But, thanks to Stack Overflow, you can find a handy example on Unit Testing functionality for Kendo UI: http://stackoverflow.com/questions/14667911/unit-testing-asp-net-mvc4-controller-with-kendo

However, I prefer to use MSTest as my Unit Testing framework and having a few things outlined in the Unit Test to make the Unit Test a bit clearer as follows:

[TestMethod()]
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
var kendoResultData = jsonResult.Data as DataSourceResult;
//... which you can then cast DataSourceResult.Data as
//the return type you are trying to test
var results = kendoResultData.Data as List<PharmacyLocation>;
Assert.IsInstanceOfType(result, typeof(List<PharmacyLocation>));
Assert.AreEqual(10, results.Count);
}

No comments:

Post a Comment