Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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# 使用Moq时出现奇怪的“对象引用未设置为对象实例”错误_C#_Unit Testing_Moq_Asp.net Web Api2 - Fatal编程技术网

C# 使用Moq时出现奇怪的“对象引用未设置为对象实例”错误

C# 使用Moq时出现奇怪的“对象引用未设置为对象实例”错误,c#,unit-testing,moq,asp.net-web-api2,C#,Unit Testing,Moq,Asp.net Web Api2,我正在尝试运行我的测试,但是我得到的对象引用没有设置为对象的实例。有什么想法吗?我正在使用最小起订量 试验方法: // Arrange Mock<ICustomerRepository> CustomerRepo = new Mock<ICustomerRepository>(); Customer NewCustomer = new Customert() { ID = 123456789, Date = DateTime.Now };

我正在尝试运行我的测试,但是我得到的对象引用没有设置为对象的实例。有什么想法吗?我正在使用最小起订量

试验方法:

     // Arrange
    Mock<ICustomerRepository> CustomerRepo = new Mock<ICustomerRepository>();
    Customer NewCustomer = new Customert() { ID = 123456789, Date = DateTime.Now };
    CustomerRepo.Setup(x => x.Add()).Returns(NewCustomer);
    var Controller = new CustomerController(CustomerRepo.Object, new Mock<IProductRepository>().Object);

    // Act
    IHttpActionResult actionResult = Controller.CreateCustomer();

当您设置Moq时,您需要另外配置HttpContext,否则您的请求将为null。您可以在测试用例开始时调用的控制器中的函数中设置它,例如:

private Mock<ControllerContext> GetContextBase()
{
    var fakeHttpContext = new Mock<HttpContextBase>();
    var request = new Mock<HttpRequestBase>();
    var response = new Mock<HttpResponseBase>();
    var session = new MockHttpSession();
    var server = new MockServer();
    var parms = new RequestParams();
    var uri = new Uri("http://TestURL/Home/Index");

    var fakeIdentity = new GenericIdentity("DOMAIN\\username");
    var principal = new GenericPrincipal(fakeIdentity, null);

    request.Setup(t => t.Params).Returns(parms);
    request.Setup(t => t.Url).Returns(uri);
    fakeHttpContext.Setup(t => t.User).Returns(principal);
    fakeHttpContext.Setup(ctx => ctx.Request).Returns(request.Object);
    fakeHttpContext.Setup(ctx => ctx.Response).Returns(response.Object);
    fakeHttpContext.Setup(ctx => ctx.Session).Returns(session);
    fakeHttpContext.Setup(ctx => ctx.Server).Returns(server);

    var controllerContext = new Mock<ControllerContext>();
    controllerContext.Setup(t => t.HttpContext).Returns(fakeHttpContext.Object);

    return controllerContext;
}
这将为您提供一个要使用的请求对象:

[编辑]

您需要的名称空间包括:

using System.Security.Principal;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

当您设置Moq时,您需要另外配置HttpContext,否则您的请求将为null。您可以在测试用例开始时调用的控制器中的函数中设置它,例如:

private Mock<ControllerContext> GetContextBase()
{
    var fakeHttpContext = new Mock<HttpContextBase>();
    var request = new Mock<HttpRequestBase>();
    var response = new Mock<HttpResponseBase>();
    var session = new MockHttpSession();
    var server = new MockServer();
    var parms = new RequestParams();
    var uri = new Uri("http://TestURL/Home/Index");

    var fakeIdentity = new GenericIdentity("DOMAIN\\username");
    var principal = new GenericPrincipal(fakeIdentity, null);

    request.Setup(t => t.Params).Returns(parms);
    request.Setup(t => t.Url).Returns(uri);
    fakeHttpContext.Setup(t => t.User).Returns(principal);
    fakeHttpContext.Setup(ctx => ctx.Request).Returns(request.Object);
    fakeHttpContext.Setup(ctx => ctx.Response).Returns(response.Object);
    fakeHttpContext.Setup(ctx => ctx.Session).Returns(session);
    fakeHttpContext.Setup(ctx => ctx.Server).Returns(server);

    var controllerContext = new Mock<ControllerContext>();
    controllerContext.Setup(t => t.HttpContext).Returns(fakeHttpContext.Object);

    return controllerContext;
}
这将为您提供一个要使用的请求对象:

[编辑]

您需要的名称空间包括:

using System.Security.Principal;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

在Mock的声明中将type设置为Mock会给我带来问题

Mock CustomerRepo=新的Mock; 一旦您删除了以下内容,可能会避免此问题

var CustomerRepo=新模拟;
在Mock的声明中将type设置为Mock会给我带来问题

Mock CustomerRepo=新的Mock; 一旦您删除了以下内容,可能会避免此问题

var CustomerRepo=新模拟;
调试时,哪个对象为空?您是否设置了请求对象?creatcustomer方法中的NewCustomer对象使用TestMethod中设置的ID和日期填充,如果在调试模式下运行测试,则NewCustomer和Request不为null?使用Moq时,您需要配置您的HttpContext,包括您的请求对象。NewCustomer不为null,调试时请求为null,哪个对象为null?您是否设置了请求对象?creatcustomer方法中的NewCustomer对象使用TestMethod中设置的ID和日期填充,如果在调试模式下运行测试,则NewCustomer和Request不为null?使用Moq时,需要配置HttpContext,包括请求对象。NewCustomer不为null,请求为null。您能说明所需的名称空间吗?请再说一遍。最后一件事:我在var fakeIdentity=new GenericEntityDomain\username行上得到了这个无法识别的转义序列错误;我取出了我的测试用户名,您应该在域\\用户名中包含您自己的,并使用双反斜杠转义。我编辑了答案。您能说明所需的名称空间吗?请再说一遍。最后一件事:我在var fakeIdentity=new GenericEntityDomain\username行上得到了这个无法识别的转义序列错误;我取出了我的测试用户名,您应该在域\\用户名中包含您自己的,并使用双反斜杠转义。我编辑了答案。
using System.Security.Principal;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;