Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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# 值不能为null。参数名称:请求_C#_Unit Testing_Nunit_Httprequest_Httpresponsemessage - Fatal编程技术网

C# 值不能为null。参数名称:请求

C# 值不能为null。参数名称:请求,c#,unit-testing,nunit,httprequest,httpresponsemessage,C#,Unit Testing,Nunit,Httprequest,Httpresponsemessage,我正在使用nunit创建一个单元测试,所有这些代码在运行时都运行良好 我有下面这个受保护的HttpResponseMessage代码,当它返回时,我的控制器正在调用它 但是,有一个错误: 显示“值不能为空。参数名称:请求” 当我检查请求时,它实际上是null 问题: 我将如何编写单元测试代码以返回HttpResponseMessage 错误显示在此行中: protected HttpResponseMessage Created<T>(T result) => Reques

我正在使用nunit创建一个单元测试,所有这些代码在运行时都运行良好

我有下面这个受保护的
HttpResponseMessage
代码,当它返回时,我的控制器正在调用它

但是,有一个错误:

显示“值不能为空。参数名称:请求”

当我检查请求时,它实际上是
null

问题: 我将如何编写单元测试代码以返回
HttpResponseMessage

错误显示在此行中:

  protected HttpResponseMessage Created<T>(T result) => Request.CreateResponse(HttpStatusCode.Created, Envelope.Ok(result));

我认为发生的情况是,当您新建控制器时,您没有实例化或分配您的请求属性(
HttpRequestMessage
)。我认为在通过单元测试调用Api方法之前,必须指定请求

您可能还需要配置(
HttpConfiguration
):


让我知道这是否有效。

您获得以下信息的原因:

System.Web.Http.dll中发生“System.ArgumentNullException”类型的异常,但未在用户代码中处理 其他信息:值不能为空

是因为
请求
对象为
null

解决方案是在测试中创建控制器实例,例如:

    var myApiController = new MyApiController
    {
        Request = new System.Net.Http.HttpRequestMessage(),
        Configuration = new HttpConfiguration()
    };
这样,在创建
MyApiController
类的新实例时,我们正在初始化
Request
对象。此外,还需要提供关联的配置对象

最后,Api控制器的单元测试示例如下:

[TestClass]
public class MyApiControllerTests
{
    [TestMethod]
    public void CreateEmployee_Returns_HttpStatusCode_Created()
    {
        // Arrange
        var controller = new MyApiController
        {
            Request = new System.Net.Http.HttpRequestMessage(),
            Configuration = new HttpConfiguration()
        };

        var employee = new CreateEmployeeModel
        {
            Id = 1
        };

        // Act
        var response = controller.CreateEmployee(employee);

        // Assert
        Assert.AreEqual(response.StatusCode, HttpStatusCode.Created);
    }
}

此外,如果控制器有注入,则可以执行以下操作:

var controller= new MyController(injectionA, injectionB, injectionC)
{
    Request = new HttpRequestMessage(),
    Configuration = new HttpConfiguration()
};

我发现它们现在都很容易理解。

这对我来说很有效:MyController controller=new MyController(){Request=new System.Net.Http.HttpRequestMessage()};
[TestClass]
public class MyApiControllerTests
{
    [TestMethod]
    public void CreateEmployee_Returns_HttpStatusCode_Created()
    {
        // Arrange
        var controller = new MyApiController
        {
            Request = new System.Net.Http.HttpRequestMessage(),
            Configuration = new HttpConfiguration()
        };

        var employee = new CreateEmployeeModel
        {
            Id = 1
        };

        // Act
        var response = controller.CreateEmployee(employee);

        // Assert
        Assert.AreEqual(response.StatusCode, HttpStatusCode.Created);
    }
}
var controller= new MyController(injectionA, injectionB, injectionC)
{
    Request = new HttpRequestMessage(),
    Configuration = new HttpConfiguration()
};