Skip to content

Instantly share code, notes, and snippets.

@xiangzhuyuan
Last active June 12, 2020 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiangzhuyuan/eebe0dd0b321c7725698 to your computer and use it in GitHub Desktop.
Save xiangzhuyuan/eebe0dd0b321c7725698 to your computer and use it in GitHub Desktop.
测试中怎么 stub 外部服务 ref: http://qiita.com/xiangzhuyuan/items/9f3301cd30e09369e7d1
# spec/spec_helper.rb
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
# spec/features/external_request_spec.rb
require 'spec_helper'
feature 'External request' do
it 'queries FactoryGirl contributors on GitHub' do
uri = URI('https://api.github.com/repos/thoughtbot/factory_girl/contributors')
response = Net::HTTP.get(uri)
expect(response).to be_an_instance_of(String)
end
end
$ rspec spec/features/external_request_spec.rb
F
Failures:
1) External request queries FactoryGirl contributors on GitHub
Failure/Error: response = Net::HTTP.get(uri)
WebMock::NetConnectNotAllowedError:
Real HTTP connections are disabled.
Unregistered request: GET https://api.github.com/repos/thoughtbot/factory_girl/contributors
with headers {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Host'=>'api.github.com',
'User-Agent'=>'Ruby'
}
You can stub this request with the following snippet:
stub_request(:get, "https://api.github.com/repos/thoughtbot/factory_girl/contributors").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host'=>'api.github.com', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})
============================================================
# ./spec/features/external_request_spec.rb:8:in `block (2 levels) in <top (required)>'
Finished in 0.00499 seconds
1 example, 1 failure
# spec/spec_helper.rb
RSpec.configure do |config|
config.before(:each) do
stub_request(:get, /api.github.com/).
with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(status: 200, body: "stubbed response", headers: {})
end
end
$ rspec spec/features/external_request_spec.rb
.
Finished in 0.01116 seconds
1 example, 0 failures
# spec/spec_helper.rb
RSpec.configure do |config|
config.before(:each) do
stub_request(:any, /api.github.com/).to_rack(FakeGitHub)
end
end
# spec/support/fake_github.rb
require 'sinatra/base'
class FakeGitHub < Sinatra::Base
get '/repos/:organization/:project/contributors' do
json_response 200, 'contributors.json'
end
private
def json_response(response_code, file_name)
content_type :json
status response_code
File.open(File.dirname(__FILE__) + '/fixtures/' + file_name, 'rb').read
end
end
# spec/support/fixtures/contributors.json
[
{
"login": "joshuaclayton",
"id": 1574,
"avatar_url": "https://2.gravatar.com/avatar/786f05409ca8d18bae8d59200156272c?d=https%3A%2F%2Fidenticons.github.com%2F0d4f4805c36dc6853edfa4c7e1638b48.png",
"gravatar_id": "786f05409ca8d18bae8d59200156272c",
"url": "https://api.github.com/users/joshuaclayton",
"html_url": "https://github.com/joshuaclayton",
"followers_url": "https://api.github.com/users/joshuaclayton/followers",
"following_url": "https://api.github.com/users/joshuaclayton/following{/other_user}",
"gists_url": "https://api.github.com/users/joshuaclayton/gists{/gist_id}",
"starred_url": "https://api.github.com/users/joshuaclayton/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/joshuaclayton/subscriptions",
"organizations_url": "https://api.github.com/users/joshuaclayton/orgs",
"repos_url": "https://api.github.com/users/joshuaclayton/repos",
"events_url": "https://api.github.com/users/joshuaclayton/events{/privacy}",
"received_events_url": "https://api.github.com/users/joshuaclayton/received_events",
"type": "User",
"site_admin": false,
"contributions": 377
}
]
require 'spec_helper'
feature 'External request' do
it 'queries FactoryGirl contributors on GitHub' do
uri = URI('https://api.github.com/repos/thoughtbot/factory_girl/contributors')
# 看好,这里是 JSON.load 坑爹!!!
response = JSON.load(Net::HTTP.get(uri))
expect(response.first['login']).to eq 'joshuaclayton'
end
end
$ rspec spec/features/external_request_spec.rb
.
Finished in 0.04713 seconds
1 example, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment