Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9290729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:29:12+00:00 2026-06-18T20:29:12+00:00

I am trying to unit test an ASP.NET MVC 4 controller that is returning

  • 0

I am trying to unit test an ASP.NET MVC 4 controller that is returning Kendo.UI.Mvc DataSource result wrapped as Json. The problem is that I cannot get to the returned the actual data .. it is always null.

The question is, how do I validate the JSON coming back from a Controller that wraps the Kendo.UI DataSourceResult.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:29:13+00:00Added an answer on June 18, 2026 at 8:29 pm

    This problem was infuriating because I could see the collection of data I wanted in VS while debugging. I have updated the test fixture – asserts on the model data can be performed.

    Basically I did the following:

    1. Cast the ActionResult as JsonResult
    2. Using the dynamic type, get the “data” from the JsonResult which returned Kendo.Mvc.UI.DataSourceResult. Prior to using the dynamic type, only null was returned. (Note to self, learn more about the dynamic type)
    3. Cast the result from step2 as the data type you want to test.

    Controller:

        public ActionResult EditRead([DataSourceRequest] DataSourceRequest request)
        {      
            return Json(GetViewModel().ToDataSourceResult(request));           
        }
    

    Unit Test:

        [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);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to unit test a page in my ASP.NET MVC application that, when
I am trying to get a ASP.NET Unit Test running and I keep getting
I am trying to unit test a controller action that uses UpdateModel but I
I'm trying to test the Data values that are returned from an ASP.NET MVC3
I'm trying to add unit testing to an ASP.NET MVC application I have built.
I am trying to write a unit test for a controller that has a
I'm trying to get my head around how one would unit test an ASP.NET
We are trying to write Unit Tests in our ASP.Net MVC project. Some of
Trying to unit test a web application using mstest I have that problem with
I'm trying to unit test a private method that I have attached to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.