0

我有一个这样声明的方法:

public ActionResult Request(Request request)
{

该方法是使用 JavaScript 调用的,但传入的是一个字符串。反序列化会自动发生。

现在我想对它进行单元测试,以模仿 JavaScript 将传入的内容,即字符串。如何使用字符串而不是请求进行单元测试?当我创建我的单元测试时,它希望我传入反序列化类型,这不是世界末日,但如果我可以复制从客户端发送的字符串请求并用它进行测试,那就太好了.

这甚至有可能......强制通常发生的自动反序列化?

  TrackController c = new TrackController();
  c.Request(jsonString);
4

1 回答 1

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#

于 2013-11-07T01:28:25.370 回答