Created
September 18, 2008 01:21
-
-
Save wycats/11348 to your computer and use it in GitHub Desktop.
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
Spec::Matchers.create(:have_xpath) do | |
matches do |rack, xpath| | |
document = rack.body | |
if rack.status < 200 || rack.status >= 300 | |
@error_text = rack.body | |
false | |
else | |
@document = case document | |
when LibXML::XML::Document, LibXML::XML::Node | |
document | |
when StringIO | |
LibXML::XML::HTMLParser.string(document.string).parse | |
else | |
LibXML::XML::HTMLParser.string(document).parse | |
end | |
begin | |
[email protected](xpath).empty? | |
rescue LibXML::XML::XPath::InvalidPath | |
@bad_xpath = true | |
false | |
end | |
end | |
end | |
message do |not_string, rack, xpath| | |
if @bad_xpath | |
"the XPath '#{xpath}' was invalid" | |
elsif @error_text | |
"there was an error on your page:\n#{@error_text}" | |
else | |
"expected the following text #{not_string}to match the xpath '#{xpath}':\n\n#{@document}" | |
end | |
end | |
end | |
Spec::Matchers.create(:redirect_to) do | |
expected_value do |location| | |
url(location) if location.is_a?(Symbol) | |
end | |
matches do |rack, location| | |
rack.status == 302 && rack.headers["Location"] == location | |
end | |
negative_failure_message do |rack, location| | |
"Expected #{describe_request(rack)} not to redirect to " \ | |
"'#{location}' but it did." | |
end | |
failure_message do |rack, location| | |
if rack.status != 302 | |
"Expected #{describe_request(rack)} to be a redirect, but " \ | |
"it returned status code #{rack.status}." | |
elsif rack.headers["Location"] != location | |
"Expected #{describe_request(rack)} to redirect to " \ | |
"'#{location}', but it redirected to #{rack.headers["Location"]}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment