我正在尝试使用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]) endAnd 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!
更多推荐
发布评论