Class: Suhyo::ViewMatchers::HaveRestfulForm

Inherits:
Webrat::Matchers::HaveSelector
  • Object
show all
Defined in:
lib/suhyo/view_matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(method, options = {}, &block) ⇒ HaveRestfulForm

Returns a new instance of HaveRestfulForm.

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/suhyo/view_matchers.rb', line 85

def initialize(method, options = {}, &block)
	raise(ArgumentError, "Expected options to be a Hash, but got #{options.inspect}") unless options.is_a?(Hash)
	@options = options
	@method = method.to_s.downcase
	unless @method == 'get' or @method == 'post' or @method == 'put' or @method == 'delete'
		raise(ArgumentError, "Expected method to be 'get', 'post', 'put', or 'delete', but got #{method.inspect}")
	end
	# browser_method is the method the browser actually uses, either GET or POST
	@browser_method = (method == 'get') ? 'get' : 'post'
	if ancestors = @options.delete(:ancestors)
		@expected = ancestors + ' form'
	else
		@expected = 'form'
	end
	@options.merge!(:method => @browser_method)
	@block = block
end

Instance Method Details

#attributes_messageObject



134
135
136
137
138
# File 'lib/suhyo/view_matchers.rb', line 134

def attributes_message
	attrs = @options.dup
	attrs.delete(:method)
	attrs.empty? ? nil : ' with attributes ' + attrs.inspect
end

#failure_messageObject

Messages



126
127
128
# File 'lib/suhyo/view_matchers.rb', line 126

def failure_message
	"expected following output to contain a #{@method.upcase} form#{attributes_message}:\n\n#{@document}"
end

#matches(stringlike) ⇒ Object



103
104
105
106
# File 'lib/suhyo/view_matchers.rb', line 103

def matches(stringlike)
	# Cache it, because we're going to use it again in matches_hidden_field?
	@matches ||= super(stringlike)
end

#matches?(stringlike, &block) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/suhyo/view_matchers.rb', line 108

def matches?(stringlike, &block)
	super(stringlike, &block) and matches_hidden_field?(stringlike)
end

#matches_hidden_field?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/suhyo/view_matchers.rb', line 112

def matches_hidden_field?(stringlike)
	form = matches(stringlike)
	case @method
	when 'get', 'post'
		!::Webrat::Matchers::HaveSelector.new('input', :name => '_method').matches?(form)
	when 'put'
		::Webrat::Matchers::HaveSelector.new('input', :name => '_method', :value => 'put').matches?(form)
	when 'delete'
		::Webrat::Matchers::HaveSelector.new('input', :name => '_method', :value => 'delete').matches?(form)
	end
end

#negative_failure_messageObject



130
131
132
# File 'lib/suhyo/view_matchers.rb', line 130

def negative_failure_message
	"expected following output to omit a #{@method.upcase} form#{attributes_message}:\n\n#{@document}"
end