asp.net mvc rhino 模拟 httprequest 值
我正在尝试编写一个测试,我可以模拟 HttpRequestBase 以返回这样的发布值吗?我怎样才能实现这个目标?
var collection = new NameValueCollection();
collection.Add("Id", "1");
collection.Add("UserName", "");
var mocks = new MockRepository();
using (mocks.Record())
{
Expect.Call(requestBase.Params).Return(collection);
}
基本上我有一个要求,要求我模拟请求发布参数而不是表单值,因为 UI 客户端不是 html 表单,有什么想法如何伪造/模拟 httprequest 发布参数吗?返回类型是 nameVaueCollection
I'm trying to write a test can I mock a HttpRequestBase to return post values like this? How can I achieve this?
var collection = new NameValueCollection();
collection.Add("Id", "1");
collection.Add("UserName", "");
var mocks = new MockRepository();
using (mocks.Record())
{
Expect.Call(requestBase.Params).Return(collection);
}
Basically I have a requirement that rquires me to mock request post parameters as opposed to form values as the UI client is not a html form, any ideas how to fake/mock the httprequest post params? the return type is a nameVaueCollection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不会喜欢听这个,但你处理这个问题的方式是错误的。您应该使用模型作为输入,并让模型绑定器填充属性,而不是直接从请求参数中获取值。这将使您的生活(包括模拟)变得更加容易,因为您将提供一个模型作为操作方法的参数,而不必模拟 HttpRequest 对象。
如果您必须使用参数,那么至少我建议您在模拟中使用 AAA 语法。
You're not going to like hearing this, but you're going about this the wrong way. You should be using models for your inputs and letting the model binder fill in the properties rather than getting the values out of the request parameters directly. This will make your life, including mocking much easier, since you'll be supplying a model as a parameter to the action method rather than having to mock up the HttpRequest object.
If you must use the parameters, then at least I suggest you use the AAA syntax for your mocks.