1
# If you have a global stories helper, move this line there:
2
include AuthenticatedTestHelper
4
# Most of the below came out of code from Ben Mabey
5
# http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/
7
# These allow exceptions to come through as opposed to being caught and having non-helpful responses returned.
8
ActionController::Base.class_eval do
10
perform_action_without_rescue
13
Dispatcher.class_eval do
14
def self.failsafe_response(output, status, exception = nil)
20
# Sugar for turning a story's attribute list into list, array, etc.
23
def ToFooFromStory.fix_key key
24
key.downcase.gsub(/\s+/, '_')
26
def ToFooFromStory.fix_value value
30
when value =~ /^'(.*)'$/ then value = $1
31
when value =~ /^"(.*)"$/ then value = $1
32
when value == 'nil!' then value = nil
33
when value == 'non-nil!' then value = be_nil
34
when value =~ /^#\{(.*)\}$/ then value = eval($1)
38
# Converts a key: value list found in the steps into a hash.
40
# ISBN: '0967539854' and comment: 'I love this book' and Quality rating: '4'
41
# # => {"quality_rating"=>"4", "isbn"=>"0967539854", "comment"=>"I love this book"}
42
def to_hash_from_story
43
hsh = self.split(/,? and |, /).inject({}) do |hash_so_far, key_value|
44
key, value = key_value.split(":")
45
if !value then warn "Couldn't understand story '#{self}': only understood up to the part '#{hash_so_far.to_yaml}'" end
46
hash_so_far.merge(ToFooFromStory::fix_key(key) => ToFooFromStory::fix_value(value))
49
# Coverts an attribute list found in the steps into an array
51
# login, email, updated_at, and gravatar
52
# # => ['login', 'email', 'updated_at', 'gravatar']
53
def to_array_from_story
54
self.split(/,? and |, /).map do |value|
55
ToFooFromStory::fix_value(value)
60
include ToFooFromStory
63
def instantize(string)
64
instance_variable_get("@#{string}")
68
# Spew response onto screen -- painful but scrolling >> debugger
71
# note that @request and @template won't to_yaml and that @session includes @cgi
72
response_methods = response.instance_variables - ['@request', '@template', '@cgi']
73
request_methods = response.request.instance_variables - ['@session_options_with_string_keys', '@cgi', '@session']
74
response_methods.map!{|attr| attr.gsub(/^@/,'')}.sort!
75
request_methods.map!{ |attr| attr.gsub(/^@/,'')}.sort!
77
response.instance_values.slice(*response_methods).to_yaml,
79
response.request.instance_values.slice(*request_methods).to_yaml,