Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ModelBinder应用于MVC ActionResult参数的单元测试?_C#_Asp.net Mvc_Unit Testing_Nunit_Moq - Fatal编程技术网

C# ModelBinder应用于MVC ActionResult参数的单元测试?

C# ModelBinder应用于MVC ActionResult参数的单元测试?,c#,asp.net-mvc,unit-testing,nunit,moq,C#,Asp.net Mvc,Unit Testing,Nunit,Moq,我有一个控制器,比如: 公共行动结果索引([ModelBinder(typeof(MyBinder))]int?MyId) 我想通过Nunit+Moq+AutoFixture创建一个单元测试,以确保MyId参数是用MyBinder修饰的,看起来像是一个有效的单元测试,因为如果删除它,代码将按预期停止工作。显然,实际定制模型绑定器的测试是单独进行的 我希望这类似于测试属性是否用特定属性修饰,如下面所示,但找不到如何以这种方式访问参数: private readonly PropertyInfo _

我有一个控制器,比如:

公共行动结果索引([ModelBinder(typeof(MyBinder))]int?MyId)

我想通过Nunit+Moq+AutoFixture创建一个单元测试
,以确保MyId参数是用MyBinder修饰的,看起来像是一个有效的单元测试,因为如果删除它,代码将按预期停止工作。显然,实际定制模型绑定器的测试是单独进行的

我希望这类似于测试属性是否用特定属性修饰,如下面所示,但找不到如何以这种方式访问参数:

private readonly PropertyInfo _SomeProp = typeof(AddressViewModel).GetProperty("SomeProp");<br>
_SomeProp.Should().BeDecoratedWith&lt;DisplayAttribute&gt;();
private readonly PropertyInfo\u SomeProp=typeof(AddressViewModel).GetProperty(“SomeProp”)
_SomeProp.Should().BeDecoratedWithDisplayAttribute();
这相当简单

这是MVC web应用程序:

  public class MyBinder : IModelBinder
  {
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
      return default(int);
    }
  }

  public class DefaultController : Controller
  {
    public ActionResult Index([ModelBinder(typeof(MyBinder))] int? MyId)
    {
      return null;
    }
  }
这是我们的NUnit测试:

[Test]
public void Index_HasParamWithBinderAttribute()
{
  var targetType = typeof(DefaultController);
  var targetAction = targetType.GetMethod("Index", BindingFlags.Instance | BindingFlags.Public); // if there is an exception below, someone removed action from the controller

  var targetParams = targetAction.GetParameters().FirstOrDefault(x => x.Name == "MyId"); // if there is an exception below, then someone renamed the action argument

  Assert.That(targetParams.ParameterType, Is.EqualTo(typeof(int?))); // if this fails, then someone changed the type of parameter
  Assert.That((targetParams.GetCustomAttributes(true).FirstOrDefault() as ModelBinderAttribute).BinderType, Is.EqualTo(typeof(MyBinder))); // if this fails, then either there is no more a modelbinder or the type is wrong
}