c# - 在 ASP.MVC 中测试具有相同名称的 Controller 中的 GET/POST 方法

标签 c# asp.net-mvc unit-testing

我有两种方法

[HttpGet]
public ActionResult Edit(int? id)
{
   // do stuff
   return View();
}
[HttpPost]
public ActionResult Edit(Object object)
{
  //do more stuff
  return View();
}

我正在尝试测试这个方法,例如我想通过null到 Controller ,我收到歧义错误。

var controller = new Controller();
controller.Edit(null); // ambiguity error

如何区分我要调用 GET 还是 POST 方法?

最佳答案

您还可以使用参数化调用。

[HttpGet]
public ActionResult Edit(int? id)
{
   return View();
}

[HttpPost]
public ActionResult Edit(Object myObject)
{
  return View();
}

.

var controller = new Controller();

controller.Edit(myObject: null);
controller.Edit(id: null);

关于c# - 在 ASP.MVC 中测试具有相同名称的 Controller 中的 GET/POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010432/

相关文章:

javascript - Css/Js 的缩小 - 正确的方法?

c# - 单元测试 async void 事件处理程序

c# - ConcurrentBag - 添加多个项目?

c# - 有用或不重要的双重接口(interface)的例子

c# - 如何获取当前用户,以及如何在 MVC5 中使用 User 类?

java - 如何使用 try-with-resources 测试方法

java - Controller 的 Spring Boot 测试 @WebMvcTest 似乎在上下文中加载其他 Controller

c# - 即使 DropDownList 已禁用其 View ,SelectedValue 仍应返回一个值

c# - 如何比 OpenPop.dll 更快地解析电子邮件

c# - 如何使用C#将图像保存到数据库中