asp.net mvc rhino 模拟 httprequest 值

发布于 2024-09-08 16:12:21 字数 433 浏览 1 评论 0原文

我正在尝试编写一个测试,我可以模拟 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-09-15 16:12:26

你不会喜欢听这个,但你处理这个问题的方式是错误的。您应该使用模型作为输入,并让模型绑定器填充属性,而不是直接从请求参数中获取值。这将使您的生活(包括模拟)变得更加容易,因为您将提供一个模型作为操作方法的参数,而不必模拟 HttpRequest 对象。

var model = new UserModel { ID = 1, UserName = string.Empty };

var controller = new FooController();

var result = controller.FooAction( model );

如果您必须使用参数,那么至少我建议您在模拟中使用 AAA 语法。

var request = MockRepository.GenerateMock<HttpRequestBase>();
var context = MockRepository.GenerateMock<HttpContextBase>();

var collection = new NameValueCollection();   
collection.Add("Id", "1");   
collection.Add("UserName", "");

context.Expect( c => c.Request ).Return( request ).Repeat.Any();
request.Expect( r => r.Params ).Return( collection ).Repeat.Any()

var controller = new FooController();
controller.ControllerContext = new ControllerContext( context, new RouteData(), controller );

var result = controller.FooAction();

...

context.VerifyAllExpectations();
request.VerifyAllExpectations();

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.

var model = new UserModel { ID = 1, UserName = string.Empty };

var controller = new FooController();

var result = controller.FooAction( model );

If you must use the parameters, then at least I suggest you use the AAA syntax for your mocks.

var request = MockRepository.GenerateMock<HttpRequestBase>();
var context = MockRepository.GenerateMock<HttpContextBase>();

var collection = new NameValueCollection();   
collection.Add("Id", "1");   
collection.Add("UserName", "");

context.Expect( c => c.Request ).Return( request ).Repeat.Any();
request.Expect( r => r.Params ).Return( collection ).Repeat.Any()

var controller = new FooController();
controller.ControllerContext = new ControllerContext( context, new RouteData(), controller );

var result = controller.FooAction();

...

context.VerifyAllExpectations();
request.VerifyAllExpectations();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文