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

时间:2014-06-20 03:11:26

标签: c# unit-testing moq asp.net-web-api2

我正在尝试运行我的测试但是我得到一个"对象引用没有设置为对象的实例"。有什么想法吗?我正在使用Moq。

测试方法:

     // 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();

CreateCustomer方法:

     Customer NewCustomer = CustomerRepository.Add();

      //ERROR OCCURS BELOW  
     return Created(Request.RequestUri + "/" + NewCustomer.ID.ToString(), new { customerID = NewCustomer.ID });

2 个答案:

答案 0 :(得分:3)

设置Moq时,需要另外配置HttpContext,否则您的Request将为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;
}

支持类别如下:

/// <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;
    }
}

最后,在Test方法的顶部,只需添加此调用:

// Arrange
HomeController controller = new HomeController();
controller.ControllerContext = GetContextBase().Object;

这将为您提供一个Request对象:)

[编辑]

您需要的名称空间是:

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

答案 1 :(得分:0)

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

Mock<ICustomerRepository> CustomerRepo = new Mock<ICustomerRepository>();

按以下方式删除后可能会避免此问题

var CustomerRepo = new Mock<ICustomerRepository>();