c# - Helper method for testing JsonResult in Nunit -


here controller returns json.

public jsonresult getvalues() {     return json(     new{         title = new {             text = "hello world"         },         xaxis = new {             type = "list of countries"             labels = new {                 rotation = 90             }         }     },jsonrequestbehavior.allowget); } 

and in nunit, testing follows:

  [test]     public void testgetvalues()     {         var controller = new helloworldcontroller();         var values = controller.getvalues() jsonresult;          assert.isnotnull(data);         var title = values.data.gettype().getproperty("title")                     .getvalue(values.data,null);         var text = title.gettype().getproperty("text").getvalue(title);         assert.isnotnull(text);      } 

this works fine have test several methods has same properties require me write same thing on , on again. how write helper method can pass in controller , property want test. this:

var checktext = getjsonproperties(controllername, "data/title/text"); var checkxais = getjsonproperties(controllernmae, "data/xaxis/type"); 

how can achieve ?

i use dynamic type.

var controller = new helloworldcontroller(); dynamic values = controller.getvalues();  var title = (string)values.title; 

saves writing helper methods, may need tested!


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -