模拟Asp.net-mvc控制器上下文


69

因此,控制器上下文取决于一些asp.net内部。有什么方法可以完全模拟这些以进行单元测试?当我只需要,例如Request.HttpMethod返回“ GET”时,似乎很容易用大量设置阻塞测试。

我已经在网上看到了一些例子/帮助者,但其中有些是过时的。认为这将是保持最新和最佳状态的好地方。

我正在使用最新版本的犀牛模拟


我正在考虑这样做。但是只需要模拟数据库连接即可。我没有测试数据库映射,而是将函数移至常规类并仅在没有数据库连接的情况下测试了该函数。
MrFox

Answers:


65

使用最小起订量看起来像这样:

var request = new Mock<HttpRequestBase>();
request.Expect(r => r.HttpMethod).Returns("GET");
var mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Expect(c => c.Request).Returns(request.Object);
var controllerContext = new ControllerContext(mockHttpContext.Object
, new RouteData(), new Mock<ControllerBase>().Object);

我认为Rhino Mocks的语法相似。


17
这在MVC3中不再有效。当在RouteData上调用非虚拟,不可模拟的方法GetRequiredString时,传递空的RouteData会引发异常。
ScottKoon 2011年

3
@ScottKoon请提供其外观的演示
Jon

1
这个问题的答案向您展示了如何模拟RouteData。 stackoverflow.com/questions/986183/...
ScottKoon

21

这是一个使用MsTest和Moq的示例单元测试类,该类模拟HttpRequest和HttpResponse对象。(.NET 4.0,ASP.NET MVC 3.0)

控制器动作从请求中获取值,并在响应对象中设置http标头。其他http上下文对象可以用类似的方式模拟

[TestClass]
public class MyControllerTest
{
    protected Mock<HttpContextBase> HttpContextBaseMock;
    protected Mock<HttpRequestBase> HttpRequestMock;
    protected Mock<HttpResponseBase> HttpResponseMock;

    [TestInitialize]
    public void TestInitialize()
    {
        HttpContextBaseMock = new Mock<HttpContextBase>();
        HttpRequestMock = new Mock<HttpRequestBase>();
        HttpResponseMock = new Mock<HttpResponseBase>();
        HttpContextBaseMock.SetupGet(x => x.Request).Returns(HttpRequestMock.Object);
        HttpContextBaseMock.SetupGet(x => x.Response).Returns(HttpResponseMock.Object);
    }

    protected MyController SetupController()
    {
        var routes = new RouteCollection();
        var controller = new MyController();
        controller.ControllerContext = new ControllerContext(HttpContextBaseMock.Object, new RouteData(), controller);
        controller.Url = new UrlHelper(new RequestContext(HttpContextBaseMock.Object, new RouteData()), routes);
        return controller;
    }

    [TestMethod]
    public void IndexTest()
    {
        HttpRequestMock.Setup(x => x["x"]).Returns("1");
        HttpResponseMock.Setup(x => x.AddHeader("name", "value"));

        var controller = SetupController();
        var result = controller.Index();
        Assert.AreEqual("1", result.Content);

        HttpRequestMock.VerifyAll();
        HttpResponseMock.VerifyAll();
    }
}

public class MyController : Controller
{
    public ContentResult Index()
    {
        var x = Request["x"];
        Response.AddHeader("name", "value");
        return Content(x);
    }
}

2
非常感谢!这对我帮助很大。我希望我可以投票十次。
Maxim Eliseev

19

这是Jason链接的摘录。与Phil的方法相同,但使用犀牛。

注意:嘲笑了mockRequest的内部之前,将嘲笑mockHttpContext.Request返回mockRequest 。我相信此命令是必需的。

// create a fake web context
var mockHttpContext = MockRepository.GenerateMock<HttpContextBase>();
var mockRequest = MockRepository.GenerateMock<HttpRequestBase>();
mockHttpContext.Stub(x => x.Request).Return(mockRequest);

// tell the mock to return "GET" when HttpMethod is called
mockRequest.Stub(x => x.HttpMethod).Return("GET");            

var controller = new AccountController();

// assign the fake context
var context = new ControllerContext(mockHttpContext, 
                  new RouteData(), 
                  controller);
controller.ControllerContext = context;

// act
...

10

在MVC2中,此过程似乎已稍有更改(我正在使用RC1)。如果操作需要特定的方法([HttpPost][HttpGet]),Phil Haack的解决方案对我不起作用。在Reflector中徘徊,看起来验证这些属性的方法已更改。MVC现在的检查request.Headersrequest.Form以及request.QueryStringX-HTTP-Method-Override值。

如果您为这些属性添加模拟,它将起作用:

var request = new Mock<HttpRequestBase>();
request.Setup(r => r.HttpMethod).Returns("POST");
request.Setup(r => r.Headers).Returns(new NameValueCollection());
request.Setup(r => r.Form).Returns(new NameValueCollection());
request.Setup(r => r.QueryString).Returns(new NameValueCollection());

var mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Expect(c => c.Request).Returns(request.Object);
var controllerContext = new ControllerContext(mockHttpContext.Object, new RouteData(), new Mock<ControllerBase>().Object);

1
这对我有用,但是在MVC2 RC中,我还必须添加以下内容:request.Setup(r => r.Files).Returns(new Mock <HttpFileCollectionBase>()。Object); 否则,我会得到nullreferenceexception
Hugo Zapata 2010年

6

或者,您可以使用Typemock隔离器执行此操作,而无需完全发送假控制器:

Isolate.WhenCalled(()=>HttpContext.Request.HttpMethod).WillReturn("Get");

2

我已经完成了这个规格

public abstract class Specification <C> where C: Controller
{
    protected C controller;

    HttpContextBase mockHttpContext;
    HttpRequestBase mockRequest;

    protected Exception ExceptionThrown { get; private set; }

    [SetUp]
    public void Setup()
    {
        mockHttpContext = MockRepository.GenerateMock<HttpContextBase>();
        mockRequest = MockRepository.GenerateMock<HttpRequestBase>();

        mockHttpContext.Stub(x => x.Request).Return(mockRequest);
        mockRequest.Stub(x => x.HttpMethod).Return("GET");


        EstablishContext();
        SetHttpContext();

        try
        {
            When();
        }
        catch (Exception exc)
        {
            ExceptionThrown = exc;
        }
    }

    protected void SetHttpContext()
    {
        var context = new ControllerContext(mockHttpContext, new RouteData(), controller);
        controller.ControllerContext = context;
    }

    protected T Mock<T>() where T: class
    {
        return MockRepository.GenerateMock<T>();
    }

    protected abstract void EstablishContext();
    protected abstract void When();

    [TearDown]
    public virtual void TearDown()
    {
    }
} 

果汁在这里

[TestFixture]
public class When_invoking_ManageUsersControllers_Update :Specification   <ManageUsersController>
{
    private IUserRepository userRepository;
    FormCollection form;

    ActionResult result;
    User retUser;

    protected override void EstablishContext()
    {
        userRepository = Mock<IUserRepository>();
        controller = new ManageUsersController(userRepository);

        retUser = new User();
        userRepository.Expect(x => x.GetById(5)).Return(retUser);
        userRepository.Expect(x => x.Update(retUser));

        form = new FormCollection();
        form["IdUser"] = 5.ToString();
        form["Name"] = 5.ToString();
        form["Surename"] = 5.ToString();
        form["Login"] = 5.ToString();
        form["Password"] = 5.ToString();
    }

    protected override void When()
    {
        result = controller.Edit(5, form);
    }

    [Test]
    public void is_retrieved_before_update_original_user()
    {
        userRepository.AssertWasCalled(x => x.GetById(5));
        userRepository.AssertWasCalled(x => x.Update(retUser));
    }
}

请享用


0

我发现漫长的模拟程序会产生太多摩擦。

我们发现的最好方法-在实际项目中使用ASP.NET MVC-是将HttpContext抽象到简单地通过的IWebContext接口。然后,您可以轻松模拟IWebContext。

这是一个例子


2
您能再解释一下吗?
user40097
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.