locked
How to pass JSON file like SVC and XML to Unit Test Method RRS feed

  • Question

  • Hi,

    I'm testing API which is accepting JSON as request. I'm converting  JSON into proper C# object and save/update data into DB.

    I know that I can pass xml and CSV file to unit test case like below

     [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "C:\\temp\\xmlData1.xml", "Row", DataAccessMethod.Random)]

    My question is - How can i pass JSON string to my test method.

    Regards,

    Mitesh Sheth

    Thursday, March 27, 2014 8:14 PM

Answers

  • @Mitesh,

    Thanks for your friendly response.

    Sorry for that if I misunderstood this issue, not the real WCF SERVICES developer, but I could provide the information what I have understood about this issue.

    You convert JSON string into C# object and save the object to the DB, now you want to get the JSON string from the DB, am I right?  Actually the real issue is that how to convert C# object back to JSON string from the DB, am I right?

    If I understand this issue correctly, could you get a method to convert data to JSON String in C# or VB before you use it in your unit test?

    I just get some cases/documents which are related to the WEB API, but it shared us some suggestions about how to convert Datatable to JSON String in C# or VB. Maybe it could provide the sunshine for this issue :)

    http://stackoverflow.com/questions/17398019/how-to-convert-datatable-to-json-in-c-sharp

    http://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html

    http://weblogs.asp.net/navaidakhtar/archive/2008/07/08/converting-data-table-dataset-into-json-string.aspx

    Note: This response contains references to the third party World Wide Web sites. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites;

    Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    In addition, I also did some research about the WCF Service “Data to JSON”:

    WCF Service Converting DataSet to Json [duplicate]

    DataTable to JSON- lose extra formatting?

    If still no help, if possible, my suggestion is that you could post this issue to the specific development forum for this issue, I know this issue is also related to the unit test, but if we want to get the real method for it, I’m afraid that we would discuss this issue with the WCF services expert, and get a method to read/convert the data with custom code, and then use it in the unit test project.

    Maybe you could post this issue to WCF forum or the specific
    language development forum like C#.

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

    If there's any concern, please feel free to let me know.

    If I have misunderstood this issue, please feel free to let me know :)

    Sincerely


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 1, 2014 2:49 AM
  • Hi Mitesh,

    Thanks for your friendly response.

    I did some research, sorry for that I also couldn’t find the way to achieve it, it seems that it has that limitation.

    http://forums.asp.net/t/1383219.aspx?How+to+unit+test+the+JsonResult+

    http://www.heartysoft.com/ashic/blog/2010/5/ASPNET-MVC-Unit-Testing-JsonResult-Returning-Anonymous-Types

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites;

    Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    If possible, you could submit this feature request: http://visualstudio.uservoice.com/forums/121579-visual-studio. The Visual Studio product team is listening to user voice there. You can send your idea there and people can vote. If you submit this suggestion, I hope you could post that link here, I will help you vote it.

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 9, 2014 8:46 AM

All replies

  • Hi Mitesh,

    If possible, you could check this blog "Unit Testing The MVC JsonResult", it shared some information about testing Action Methods that return a JsonResult. Hope it could help.

    Reference:

    http://stackoverflow.com/questions/7096774/how-to-get-the-actual-json-from-a-jsonresult-object-for-a-unit-test

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 28, 2014 11:15 AM
  • Thanks for reply.

    I have WCF services not web api. I want to use to DataSource accept input data. 

    -Mitesh

    Monday, March 31, 2014 12:44 PM
  • @Mitesh,

    Thanks for your friendly response.

    Sorry for that if I misunderstood this issue, not the real WCF SERVICES developer, but I could provide the information what I have understood about this issue.

    You convert JSON string into C# object and save the object to the DB, now you want to get the JSON string from the DB, am I right?  Actually the real issue is that how to convert C# object back to JSON string from the DB, am I right?

    If I understand this issue correctly, could you get a method to convert data to JSON String in C# or VB before you use it in your unit test?

    I just get some cases/documents which are related to the WEB API, but it shared us some suggestions about how to convert Datatable to JSON String in C# or VB. Maybe it could provide the sunshine for this issue :)

    http://stackoverflow.com/questions/17398019/how-to-convert-datatable-to-json-in-c-sharp

    http://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html

    http://weblogs.asp.net/navaidakhtar/archive/2008/07/08/converting-data-table-dataset-into-json-string.aspx

    Note: This response contains references to the third party World Wide Web sites. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites;

    Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    In addition, I also did some research about the WCF Service “Data to JSON”:

    WCF Service Converting DataSet to Json [duplicate]

    DataTable to JSON- lose extra formatting?

    If still no help, if possible, my suggestion is that you could post this issue to the specific development forum for this issue, I know this issue is also related to the unit test, but if we want to get the real method for it, I’m afraid that we would discuss this issue with the WCF services expert, and get a method to read/convert the data with custom code, and then use it in the unit test project.

    Maybe you could post this issue to WCF forum or the specific
    language development forum like C#.

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

    If there's any concern, please feel free to let me know.

    If I have misunderstood this issue, please feel free to let me know :)

    Sincerely


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 1, 2014 2:49 AM
  • I know how to transfer C# object to JSON and othe way.

    I want to pass JSON string directly to my test methods. We have option to pass XML and XSL. I want to pass files which has number of JSON strings and my test method will pick up one JSON string and execute test method. 

    This way i can check all functionality of my actual class using different JSON object.

    Regards,

    Mitesh Sheth 

    Tuesday, April 8, 2014 1:10 PM
  • Hi Mitesh,

    Thanks for your friendly response.

    I did some research, sorry for that I also couldn’t find the way to achieve it, it seems that it has that limitation.

    http://forums.asp.net/t/1383219.aspx?How+to+unit+test+the+JsonResult+

    http://www.heartysoft.com/ashic/blog/2010/5/ASPNET-MVC-Unit-Testing-JsonResult-Returning-Anonymous-Types

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites;

    Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    If possible, you could submit this feature request: http://visualstudio.uservoice.com/forums/121579-visual-studio. The Visual Studio product team is listening to user voice there. You can send your idea there and people can vote. If you submit this suggestion, I hope you could post that link here, I will help you vote it.

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 9, 2014 8:46 AM