嘲讽Asp.net-MVC控制器上下文 [英] Mocking Asp.net-mvc Controller Context

查看:128
本文介绍了嘲讽Asp.net-MVC控制器上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以控制器上下文取决于一些asp.net的内部。有什么办法干净地嘲笑这些为单元测试?看起来似乎很容易堵塞测试,设置吨时,我只需要,例如,Request.HttpMethod返回GET。

So the controller context depends on some asp.net internals. What are some ways to cleanly mock these up for unit tests? Seems like its very easy to clog up tests with tons of setup when I only need, for example, Request.HttpMethod to return "GET".

我见过一些例子/佣工出来的网,但也有一些过时。估计这将是保持最新和最伟大的好地方。

I've seen some examples/helpers out on the nets, but some are dated. Figured this would be a good place to keep the latest and greatest.

我使用的是最新版本的犀牛制品的

I'm using latest version of rhino mocks

推荐答案

使用起订量,它看起来是这样的:

Using MoQ it looks something like this:

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

我觉得犀牛制品语法是相似的。

I think the Rhino Mocks syntax is similar.

这篇关于嘲讽Asp.net-MVC控制器上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆