如何使用 ASP.NET MVC 的最小起订量模拟 Request.ServerVariables?
当我开始了解模拟和现在存在的不同框架时,我刚刚学习为我的 ASP.NET MVC 进行单元测试。
检查后,我发现最小起订量似乎是最容易接受的。 截至目前,我一直在尝试模拟 Request.ServerVariables,如 读完这篇文章后,我了解到最好将它们抽象为属性。
因此:
/// <summary>
/// Return the server port
/// </summary>
protected string ServerPort
{
get
{
return Request.ServerVariables.Get("SERVER_PORT");
}
}
但我很难学习如何正确地嘲笑这一点。 我有一个家庭控制器 ActionResult 函数,它获取用户服务器信息并继续创建一个表单来获取用户信息。
我尝试使用 hanselman 的 mvcmockhelpers 类 但我不知道如何使用它。
这就是我到目前为止所拥有的...
[Test]
public void Create_Redirects_To_ProductAdded_On_Success()
{
FakeViewEngine engine = new FakeViewEngine();
HomeController controller = new HomeController();
controller.ViewEngine = engine;
MvcMockHelpers.SetFakeControllerContext(controller);
controller.Create();
var results = controller.Create();
var typedResults = results as RedirectToRouteResult;
Assert.AreEqual("", typedResults.RouteValues["action"], "Wrong action");
Assert.AreEqual("", typedResults.RouteValues["controller"], "Wrong controller");
}
问题:
- 到目前为止我仍然处于空状态 当我运行时出现异常错误 测试。那么我在这里缺少什么?
- 如果我使用 mvcmockhelpers 类,我怎么还能打电话 request.verifyall函数来确保 所有的模拟都正确设置了吗?
i'm just learning to put in unit testing for my asp.net mvc when i came to learn about the mock and the different frameworks there is out there now.
after checking SO, i found that MOQ seems to be the easiest to pick up.
as of now i'm stuck trying to mock the Request.ServerVariables, as after reading this post, i've learned that it's better to abstract them into property.
as such:
/// <summary>
/// Return the server port
/// </summary>
protected string ServerPort
{
get
{
return Request.ServerVariables.Get("SERVER_PORT");
}
}
But i'm having a hard time learning how to properly mock this.
I have a home controller ActionResult function which grabs the user server information and proceed to create a form to grab the user's information.
i tried to use hanselman's mvcmockhelpers class but i'm not sure how to use it.
this is what i have so far...
[Test]
public void Create_Redirects_To_ProductAdded_On_Success()
{
FakeViewEngine engine = new FakeViewEngine();
HomeController controller = new HomeController();
controller.ViewEngine = engine;
MvcMockHelpers.SetFakeControllerContext(controller);
controller.Create();
var results = controller.Create();
var typedResults = results as RedirectToRouteResult;
Assert.AreEqual("", typedResults.RouteValues["action"], "Wrong action");
Assert.AreEqual("", typedResults.RouteValues["controller"], "Wrong controller");
}
Questions:
- As of now i'm still getting null
exception error when i'm running the
test. So what am i missing here? - And if i use the mvcmockhelpers
class, how can i still call the
request.verifyall function to ensure
all the mocking are properly setup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以基本上我已经输入了所有参考文献并使用这个函数我能够让它工作。我还没有使用 mvcmockhelpers 类,因为我仍在尝试学习所有这些。
对于任何有兴趣了解我如何解决这个问题的人,这是我使用的代码。
so basically i've put in all the references and using this function i was able to get it working. i didn't use mvcmockhelpers class yet as i'm still trying to learn all of this.
and for anyone who's interested to see how i solved this, this is the code that i used.
由于我想模拟 HttpContext,而不是 HttpContextBase,因此我尝试找到一种将 HttpContextBase 模拟对象转换为 HttpContext 的方法,但发现这是不可能的。
所以我改变了调用我的代码的方式。
从使用
HttpContext
:到使用
HttpContextBase
:当我使用该方法时,我用HttpContextWrapper包装HttpContext,如下所示:
现在您可以按照 Dan Atkinson 的回复轻松模拟 HttpContextBase
Since I want to mock HttpContext, not HttpContextBase, I tried to find a way to convert HttpContextBase mock object to HttpContext and found out that it's impossible to do.
So I change the way to call my code.
From using
HttpContext
:To use
HttpContextBase
:And when I use the method, I wrap HttpContext with HttpContextWrapper like so:
Now you can easily mock HttpContextBase by following Dan Atkinson's reply