Created
November 11, 2013 22:35
-
-
Save steookk/7421750 to your computer and use it in GitHub Desktop.
webmock 1.5.2 stub_request + hash_including spec failing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
require 'rest-client' | |
describe WebMock::API do | |
describe '#stub_request with hash_including' do | |
context "when setting 'hash_including' with an hash" do | |
before do | |
WebMock::API.stub_request(:post, 'www.example.com'). | |
with(:body => WebMock::API.hash_including(:data => {:a => '1', :b => 'five'})). | |
to_return(:status => 200, :body => "abc", :headers => {}) | |
end | |
it "responds if the hash matches" do | |
response = RestClient.post('www.example.com', "data[a]=1&data[b]=five&x=1", | |
:content_type => 'application/x-www-form-urlencoded') | |
expect(response.body).to match('abc') | |
end | |
#hashes always become an empty hash which always matches true | |
it "SHOULD FAIL BUT IT DOESN'T" do | |
response = RestClient.post('www.example.com', "data[a]=1&data[b]=five&x=1", | |
:content_type => 'application/x-www-form-urlencoded') | |
expect(response.body).to match('abc') | |
end | |
end | |
context "when setting 'hash_including' with solitary keys" do | |
before do | |
WebMock::API.stub_request(:post, 'www.example.com'). | |
with(:body => WebMock::API.hash_including(:a, :b)). | |
to_return(:status => 200, :body => "abc", :headers => {}) | |
end | |
it "responds to any solitary keys' value" do | |
response = RestClient.post('www.example.com', "a=1&b=five", | |
:content_type => 'application/x-www-form-urlencoded') | |
expect(response.body).to match('abc') | |
end | |
#solitary keys become an empty hash which always matches true | |
it "SHOULD FAIL BUT IT DOESN'T" do | |
response = RestClient.post('www.example.com', "fail", | |
:content_type => 'application/x-www-form-urlencoded') | |
expect(response.body).to match('abc') | |
end | |
end | |
context "when setting 'hash_including' with both solitary keys and a specific hash" do | |
subject { RestClient.post('www.example.com', "a=1&b=five&data=2", | |
:content_type => 'application/x-www-form-urlencoded') } | |
#this FAILS | |
context "when using 'WebMock::API#hash_including'" do | |
it "responds to any solitary keys' value and if the hash matches" do | |
WebMock::API.stub_request(:post, 'www.example.com'). | |
with(:body => WebMock::API.hash_including(:a, :b, :data => '2')). | |
to_return(:status => 200, :body => "abc", :headers => {}) | |
expect(subject.body).to match('abc') | |
end | |
end | |
context "when using 'RSpec:Mocks::ArgumentMatchers#hash_including'" do | |
it "responds to any solitary keys' value and if the hash matches" do | |
WebMock::API.stub_request(:post, 'www.example.com'). | |
with(:body => hash_including(:a, :b, :data => '2')). | |
to_return(:status => 200, :body => "abc", :headers => {}) | |
expect(subject.body).to match('abc') | |
end | |
end | |
end | |
context "when setting an empty 'hash_including'" do | |
before do | |
WebMock::API.stub_request(:post, 'www.example.com'). | |
with(:body => WebMock::API.hash_including({})). | |
to_return(:status => 200, :body => "abc", :headers => {}) | |
end | |
it "responds to an empty body" do | |
response = RestClient.post('www.example.com', "", | |
:content_type => 'application/x-www-form-urlencoded') | |
expect(response.body).to match('abc') | |
end | |
#this FAILS because the comparison between {} and {data: ...} always returns true | |
it "raises an error as a response to a body containing some data" do | |
lambda { RestClient.post('www.example.com', "data=1", | |
:content_type => 'application/x-www-form-urlencoded') }.should raise_error | |
end | |
end | |
end #end of describe | |
end #end of describe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment