use*_*602 7 c# unit-testing moq asp.net-web-api2
我正在尝试运行我的测试,但是我收到“对象引用未设置为对象的实例”。有什么想法吗?我正在使用起订量。
测试方法:
// 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();
Run Code Online (Sandbox Code Playgroud)
创建客户方法:
Customer NewCustomer = CustomerRepository.Add();
//ERROR OCCURS BELOW
return Created(Request.RequestUri + "/" + NewCustomer.ID.ToString(), new { customerID = NewCustomer.ID });
Run Code Online (Sandbox Code Playgroud)
当你设置Moq时,你需要另外配置你的HttpContext,否则你的Request将为空。您可以在控制器中的函数中进行设置,在测试用例开始时调用该函数,例如:
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;
}
Run Code Online (Sandbox Code Playgroud)
支持类如下:
/// <summary>
/// A Class to allow simulation of SessionObject
/// </summary>
public class MockHttpSession : HttpSessionStateBase
{
Dictionary<string, object> m_SessionStorage = new Dictionary<string, object>();
public override object this[string name]
{
get {
try
{
return m_SessionStorage[name];
}
catch (Exception e)
{
return null;
}
}
set { m_SessionStorage[name] = value; }
}
}
public class RequestParams : System.Collections.Specialized.NameValueCollection
{
Dictionary<string, string> m_SessionStorage = new Dictionary<string, string>();
public override void Add(string name, string value)
{
m_SessionStorage.Add(name, value);
}
public override string Get(string name)
{
return m_SessionStorage[name];
}
}
public class MockServer : HttpServerUtilityBase
{
public override string MapPath(string path)
{
return @"C:\YourCodePathTowherever\" + path;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,在测试方法的顶部,只需添加以下调用:
// Arrange
HomeController controller = new HomeController();
controller.ControllerContext = GetContextBase().Object;
Run Code Online (Sandbox Code Playgroud)
这将为您提供一个可以使用的 Request 对象:)
[编辑]
您需要的名称空间是:
using System.Security.Principal;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14700 次 |
| 最近记录: |