当对 ASP.NET 控制器进行单元测试时,您在哪里模拟 httprequestbase?
当对 ASP.NET 控制器进行单元测试时,您不需要以某种方式模拟 httpcontextbase 吗?
我的所有控制器都继承自我编写的自定义控制器类(它只是向原始控制器类添加了一些通用属性)。 就像这样:
public class MyController : Controller
{
protected override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext context)
{
// look for a specific cookie
}
}
真的很想开始对我的控制器进行单元测试,只是不确定如何模拟控制器类和随之而来的 httpcontext。
when unit testing a asp.net controller, don't you have to somehow mock the httpcontextbase?
All my controllers inherit from a custom controller class that I wrote (it just adds some common properties to the original controller class).
So its like:
public class MyController : Controller
{
protected override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext context)
{
// look for a specific cookie
}
}
so really want to start unit testing my controllers, just unsure how I go about mocking the controller classes and the httpcontext that goes with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是如何使用 Moq 设置 Mock HttpContextBase 的示例:
其中 sut 代表被测系统 (SUT),即您希望测试的控制器。
Here's an example of how you can use Moq to set up a Mock HttpContextBase:
where
sut
represents the System Under Test (SUT), i.e. the Controller you wish to test.