Skip to content

Instantly share code, notes, and snippets.

@joshed-io
Created August 23, 2011 18:19
Show Gist options
  • Save joshed-io/1166060 to your computer and use it in GitHub Desktop.
Save joshed-io/1166060 to your computer and use it in GitHub Desktop.
Use httparty with rspec and capybara
# Simple demonstration of using httparty in an rspec test suite
# capybara's not required, but if you're otherwise using a capybara
# acceptance test suite this method complements the other drivers
# in your suite such as rack test, selenium or mechanize.
# Specifically - selenium is slow, rack test doesn't work remotely,
# and mechanize doesn't work well with pages that have AJAX/JS DOM manipulation
# Sometimes an old school get or post is what you need
# fwiw my use case for this is an rspec-driven performance test
# see more at http://blog.joshdzielak.com/blog/2011/08/23/use-httparty-with-rspec-and-capybara/
# anyway. so, within spec helper ->
require 'httparty'
class TestParty
include HTTParty
base_uri "http://localhost:3000"
#set if you want to assert redirects as successful outcomes
#may lead to faster tests - server won't be hit on the follow
no_follow true
#optional proxy support
if proxy = ENV['PROXY'] #http://localhost:8888
proxy = URI.parse(proxy)
http_proxy proxy.host, proxy.port
end
end
#as used in examples ->
describe "signup" do
it "should redirect to /account_created given valid data" do
data = {
"user[username]" => "boarse",
"user[password]" => "Pa55word",
"user[password_confirmation]" => "Pa55word"
}
begin
response = TestParty.post('/signup', :body => data)
rescue HTTParty::RedirectionTooDeep => redirection #strange, but prescribed approach
redirection.response.code.should == "302"
redirection.response.header["Location"].should =~ /account_created/
end
end
end
@joshed-io
Copy link
Author

Updated. Thanks much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment