0

I have a method that is declared as so:

public ActionResult Request(Request request)
{

The method is called using JavaScript, but a string is passed in. The deserialization happens automatically.

Now I want to unit test this mimicking what the JavaScript would be passing in, which is a string. How do I unit test using a string instead of Request? When I create my unit test, it expects me to pass in the deserialized type which isn't the end of the world, but it would be nice if I could just copy the string request that gets sent in from the client and test with that.

Is this even possible... to force the automatic deserialization that normally happens?

  TrackController c = new TrackController();
  c.Request(jsonString);
KingOfHypocrites
  • 9,316
  • 9
  • 47
  • 69

1 Answers1

1

Deserializing the json into your concrete model object is really the responsibility of the MVC model binder, so I don't think that should be included in the unit test of the controller action.

However I do see some value in testing that you are creating your requests correctly, but I think this is a better fit for an integration test.

You could potentially make http requests to your website directly, which would validate if you're passing correct json to your action.

See more here: POSTing JSON to URL via WebClient in C#

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135