Created
August 23, 2011 18:19
-
-
Save joshed-io/1166060 to your computer and use it in GitHub Desktop.
Use httparty with rspec and capybara
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The see more URI is incorrect in the comment. You get a 404. Should be http://blog.joshdzielak.com/blog/2011/08/23/use-httparty-with-rspec-and-capybara/