从RSpec发送真正的JSON帖子请求(Sending a real JSON post request from RSpec)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
从RSpec发送真正的JSON帖子请求(Sending a real JSON post request from RSpec)

我希望能够为端点响应测试远程3d party API,这就是为什么我想编写一堆本地rspec测试并定期启动它们,以查看这些端点是否按预期工作而没有重大变化。 由于我的应用程序高度依赖于这种不断变化的API,我别无选择,只能自动化我的测试。

目前我采用了常规的rspec API测试代码:

require "rails_helper" RSpec.describe "Remote request", type: :request do describe ".send request" do it ".posts valid data" do post "http://123.45.67.89/api/endpoint", params: { "job_request_id": 123456, "data": "12345", "app_secret": "12345", "options": { ... } } expect(JSON.parse response.body).to include("message" => "success") expect(response).to have_http_status(200) end end end

此代码的问题是Rspec正在访问/api/endpoint url,而不是完整的http://123.45.67.89/api/endpoint网址。 我该如何改变这种行为?

I would like to have an ability of testing a remote 3d party API for endpoint responses, which is why I would like to write a bunch of local rspec tests and launch them periodically, to see if these endpoints are working as expected with no breaking changes. Since my application is highly dependent on this constantly changing API, I have little choice left but to automate my test.

At the moment I took a regular rspec API test code:

require "rails_helper" RSpec.describe "Remote request", type: :request do describe ".send request" do it ".posts valid data" do post "http://123.45.67.89/api/endpoint", params: { "job_request_id": 123456, "data": "12345", "app_secret": "12345", "options": { ... } } expect(JSON.parse response.body).to include("message" => "success") expect(response).to have_http_status(200) end end end

The problem with this code is that Rspec is hitting the /api/endpoint url, instead of the full http://123.45.67.89/api/endpoint url. How can I change this behaviour?

最满意答案

RSpec的请求规范用于测试您自己的应用程序 - 通过发送真实的HTTP请求。 它们不用于执行远程HTTP请求(即使具有配置的monkey可能请求除localhost之外的其他主机)。

除其他事项外,请求规范将针对每个示例命中API - 这将为您提供速率限制和限制的问题。

虽然您可以尝试在请求规范中使用Net :: HTTP , Httparty或Typhoeus等HTTP库 - 您真正应该做的是重新考虑您的方法。 隔离应用程序与外部协作者之间的交互是一个好主意。

一种方法是创建使用远程API的客户端类:

class ExampleAPIClient include HTTParty base_url 'example.com' format :json def get_some_data self.class.get('/foo') end end

然后,您可以通过像普通的旧Ruby对象一样测试客户端来测试远程端点:

require 'spec_helper' RSpec.describe ExampleAPIClient do let(:client) { described_class.new } describe "#get_some_data" do let(:response) { client.get_some_data } it "should be successful" do expect(response.success?).to be_truthy end end end

这将具有限制应用程序更改的额外好处,因为如果远程api更改,则只有单个组件(客户端)应该失败。

使用客户端的其他组件可以通过对客户端进行存根来轻松地存根远程交互。

RSpec's request specs are meant for testing your own application - by sending real HTTP requests. They are not intended for performing remote HTTP requests (even if it is possible monkey with the configuration to request other hosts than localhost).

Among other things a request spec will hit the API for every example - this will give you problems with rate limiting and throttling.

While you could try using a HTTP library like Net::HTTP, Httparty or Typhoeus in a request spec - what you really should do is rethink your methodology. Its a good idea to isolate the interaction between your application and external collaborators.

One way of doing this is by creating client classes that consume the remote API:

class ExampleAPIClient include HTTParty base_url 'example.com' format :json def get_some_data self.class.get('/foo') end end

You can then test the remote endpoint by testing the client like a Plain Old Ruby Object:

require 'spec_helper' RSpec.describe ExampleAPIClient do let(:client) { described_class.new } describe "#get_some_data" do let(:response) { client.get_some_data } it "should be successful" do expect(response.success?).to be_truthy end end end

This will have the additional benefit of limiting exposure to change in your application as only a single component (the client) should fail if the remote api changes.

Other components which consume the client can stub out the remote interaction easily by stubbing the client.

更多推荐

本文发布于:2023-04-13 12:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/7ee7c737f9919e571b1801e2c2233977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帖子   JSON   RSpec   Sending   request

发布评论

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

>www.elefans.com

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