1 # This is a wrapper of assert_select for rspec.
7 class AssertSelect #:nodoc:
9 def initialize(assertion, spec_scope, *args, &block)
10 @assertion = assertion
11 @spec_scope = spec_scope
16 def matches?(response_or_text, &block)
17 if ActionController::TestResponse === response_or_text and
18 response_or_text.headers.key?('Content-Type') and
19 response_or_text.headers['Content-Type'].to_sym == :xml
20 @args.unshift(HTML::Document.new(response_or_text.body, false, true).root)
21 elsif String === response_or_text
22 @args.unshift(HTML::Document.new(response_or_text).root)
24 @block = block if block
26 @spec_scope.send(@assertion, *@args, &@block)
27 rescue ::Test::Unit::AssertionFailedError => @error
33 def failure_message; @error.message; end
34 def negative_failure_message; "should not #{description}, but did"; end
38 :assert_select => "have tag#{format_args(*@args)}",
39 :assert_select_email => "send email#{format_args(*@args)}",
45 def format_args(*args)
46 return "" if args.empty?
47 return "(#{arg_list(*args)})"
52 arg.respond_to?(:description) ? arg.description : arg.inspect
59 # response.should have_tag(*args, &block)
60 # string.should have_tag(*args, &block)
62 # wrapper for assert_select with additional support for using
63 # css selectors to set expectation on Strings. Use this in
64 # helper specs, for example, to set expectations on the results
69 # # in a controller spec
70 # response.should have_tag("div", "some text")
72 # # in a helper spec (person_address_tag is a method in the helper)
73 # person_address_tag.should have_tag("input#person_address")
75 # see documentation for assert_select at http://api.rubyonrails.org/
76 def have_tag(*args, &block)
77 AssertSelect.new(:assert_select, self, *args, &block)
80 # wrapper for a nested assert_select
82 # response.should have_tag("div#form") do
83 # with_tag("input#person_name[name=?]", "person[name]")
86 # see documentation for assert_select at http://api.rubyonrails.org/
87 def with_tag(*args, &block)
88 should have_tag(*args, &block)
91 # wrapper for a nested assert_select with false
93 # response.should have_tag("div#1") do
94 # without_tag("span", "some text that shouldn't be there")
97 # see documentation for assert_select at http://api.rubyonrails.org/
98 def without_tag(*args, &block)
99 should_not have_tag(*args, &block)
103 # response.should have_rjs(*args, &block)
105 # wrapper for assert_select_rjs
107 # see documentation for assert_select_rjs at http://api.rubyonrails.org/
108 def have_rjs(*args, &block)
109 AssertSelect.new(:assert_select_rjs, self, *args, &block)
113 # response.should send_email(*args, &block)
115 # wrapper for assert_select_email
117 # see documentation for assert_select_email at http://api.rubyonrails.org/
118 def send_email(*args, &block)
119 AssertSelect.new(:assert_select_email, self, *args, &block)
122 # wrapper for assert_select_encoded
124 # see documentation for assert_select_encoded at http://api.rubyonrails.org/
125 def with_encoded(*args, &block)
126 should AssertSelect.new(:assert_select_encoded, self, *args, &block)