使用WebMock进行存根不会拦截请求(Stubbing with WebMock not intercepting request)

编程入门 行业动态 更新时间:2024-10-26 11:19:37
使用WebMock进行存根不会拦截请求(Stubbing with WebMock not intercepting request)

我正在尝试使用WebMock将请求存根到Controller。 但是,当我创建存根时,请求不会按照我期望或希望的方式被截获。

Controller除了根据查询参数呈现JSON之外什么都不做:

def index render json: MyThing.search(params[:query]).as_json(only: [:id], methods: [:name_with_path]) end

存根如下:

mything_val = { ...json values... } stub_request(:any, mything_path).with(query: { "query" => "a+thing" }).to_return(body: mything_val, status: 200) page.find('.MyThingInput > input').set('a thing') # Note: I've tried this with and without the `query:` parameter, as well as with and without specifying header info.

这是触发React组件。 它的作用是,当一个或多个单词输入到输入中时,它会向输入的值发送一个带有输入值的mything_path的AJAX请求,该输出值作为JSON返回几个关于用户可能含义的建议。 它们位于.MyThingInput-wrapper中的li元素中。

在spec文件中,我包括:

require 'support/feature_helper' require 'support/feature_matchers' require 'webmock/rspec' WebMock.disable_net_connect!

然而,当我将文本输入到React组件中时实际发生的是,无论WebMock存根是什么,它都会触及Controller,发出数据库请求,并且由于测试环境的某些限制而失败。 我对这应该如何工作的理解是,当向mything_url发出请求时,它应该被WebMock截获,它将返回我预先定义的值,并且根本不会触及Controller。

我的猜测是,我在某种程度上嘲笑错误的URI,但老实说,此时我真的很不确定。 任何和所有的意见都表示赞赏,我很高兴澄清我在这里提出的任何观点。 非常感谢!

I'm attempting to stub a request to a Controller using WebMock. However, as I'm creating the stub, the request isn't being intercepted the way I'd expect or want it to be.

The Controller does nothing but render JSON based on the query parameter:

def index render json: MyThing.search(params[:query]).as_json(only: [:id], methods: [:name_with_path]) end

And the stubbing goes as follows:

mything_val = { ...json values... } stub_request(:any, mything_path).with(query: { "query" => "a+thing" }).to_return(body: mything_val, status: 200) page.find('.MyThingInput > input').set('a thing') # Note: I've tried this with and without the `query:` parameter, as well as with and without specifying header info.

This is triggering a React component. What it does is, when a word or words are entered into the input, it sends an AJAX request to mything_path with the inputted value, which returns as JSON several suggestions as to what the user might mean. These are in a li element within .MyThingInput-wrapper.

In the spec file, I include:

require 'support/feature_helper' require 'support/feature_matchers' require 'webmock/rspec' WebMock.disable_net_connect!

What's actually happening when I input the text into the React component however is that regardless of the WebMock stub, it's hitting the Controller, making the DB request, and failing due to some restrictions of the testing environment. My understanding of how this should work is that when the request is made to mything_url, it should be intercepted by WebMock which would return the values I pre-defined, and never hit the Controller at all.

My guess is that somehow I'm mocking the wrong URI, but honestly, at this point I'm really uncertain. Any and all input is appreciated, and I'm happy to clarify any points I've made here. Thanks massively!

更多推荐

本文发布于:2023-08-06 11:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存根   WebMock   Stubbing   request   intercepting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!