dchelimsky-rspec-rails 1.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +63 -0
- data/Manifest.txt +158 -0
- data/README.txt +81 -0
- data/Rakefile +38 -0
- data/UPGRADE +7 -0
- data/generators/rspec/CHANGES +1 -0
- data/generators/rspec/rspec_generator.rb +40 -0
- data/generators/rspec/templates/all_stories.rb +4 -0
- data/generators/rspec/templates/previous_failures.txt +0 -0
- data/generators/rspec/templates/rcov.opts +2 -0
- data/generators/rspec/templates/rspec.rake +132 -0
- data/generators/rspec/templates/script/autospec +3 -0
- data/generators/rspec/templates/script/spec +4 -0
- data/generators/rspec/templates/script/spec_server +116 -0
- data/generators/rspec/templates/spec.opts +4 -0
- data/generators/rspec/templates/spec_helper.rb +47 -0
- data/generators/rspec/templates/stories_helper.rb +3 -0
- data/generators/rspec_controller/USAGE +33 -0
- data/generators/rspec_controller/rspec_controller_generator.rb +49 -0
- data/generators/rspec_controller/templates/controller_spec.rb +25 -0
- data/generators/rspec_controller/templates/helper_spec.rb +11 -0
- data/generators/rspec_controller/templates/view_spec.rb +12 -0
- data/generators/rspec_default_values.rb +19 -0
- data/generators/rspec_model/USAGE +18 -0
- data/generators/rspec_model/rspec_model_generator.rb +35 -0
- data/generators/rspec_model/templates/model_spec.rb +15 -0
- data/generators/rspec_scaffold/rspec_scaffold_generator.rb +154 -0
- data/generators/rspec_scaffold/templates/controller_spec.rb +173 -0
- data/generators/rspec_scaffold/templates/edit_erb_spec.rb +26 -0
- data/generators/rspec_scaffold/templates/helper_spec.rb +11 -0
- data/generators/rspec_scaffold/templates/index_erb_spec.rb +27 -0
- data/generators/rspec_scaffold/templates/new_erb_spec.rb +26 -0
- data/generators/rspec_scaffold/templates/routing_spec.rb +59 -0
- data/generators/rspec_scaffold/templates/show_erb_spec.rb +23 -0
- data/init.rb +9 -0
- data/lib/autotest/discover.rb +1 -0
- data/lib/autotest/rails_rspec.rb +76 -0
- data/lib/spec/rails/example/assigns_hash_proxy.rb +37 -0
- data/lib/spec/rails/example/controller_example_group.rb +256 -0
- data/lib/spec/rails/example/cookies_proxy.rb +25 -0
- data/lib/spec/rails/example/functional_example_group.rb +87 -0
- data/lib/spec/rails/example/helper_example_group.rb +166 -0
- data/lib/spec/rails/example/model_example_group.rb +14 -0
- data/lib/spec/rails/example/rails_example_group.rb +33 -0
- data/lib/spec/rails/example/render_observer.rb +93 -0
- data/lib/spec/rails/example/view_example_group.rb +183 -0
- data/lib/spec/rails/example.rb +47 -0
- data/lib/spec/rails/extensions/action_controller/base.rb +14 -0
- data/lib/spec/rails/extensions/action_controller/rescue.rb +21 -0
- data/lib/spec/rails/extensions/action_controller/test_response.rb +11 -0
- data/lib/spec/rails/extensions/action_view/base.rb +31 -0
- data/lib/spec/rails/extensions/active_record/base.rb +30 -0
- data/lib/spec/rails/extensions/object.rb +5 -0
- data/lib/spec/rails/extensions/spec/example/configuration.rb +71 -0
- data/lib/spec/rails/extensions/spec/matchers/have.rb +21 -0
- data/lib/spec/rails/extensions.rb +12 -0
- data/lib/spec/rails/interop/testcase.rb +14 -0
- data/lib/spec/rails/matchers/assert_select.rb +131 -0
- data/lib/spec/rails/matchers/change.rb +11 -0
- data/lib/spec/rails/matchers/have_text.rb +57 -0
- data/lib/spec/rails/matchers/include_text.rb +54 -0
- data/lib/spec/rails/matchers/redirect_to.rb +113 -0
- data/lib/spec/rails/matchers/render_template.rb +90 -0
- data/lib/spec/rails/matchers.rb +31 -0
- data/lib/spec/rails/mocks.rb +132 -0
- data/lib/spec/rails/story_adapter.rb +79 -0
- data/lib/spec/rails/version.rb +15 -0
- data/lib/spec/rails.rb +15 -0
- data/spec/rails/autotest/mappings_spec.rb +36 -0
- data/spec/rails/example/assigns_hash_proxy_spec.rb +70 -0
- data/spec/rails/example/configuration_spec.rb +83 -0
- data/spec/rails/example/controller_isolation_spec.rb +62 -0
- data/spec/rails/example/controller_spec_spec.rb +272 -0
- data/spec/rails/example/cookies_proxy_spec.rb +74 -0
- data/spec/rails/example/example_group_factory_spec.rb +112 -0
- data/spec/rails/example/helper_spec_spec.rb +161 -0
- data/spec/rails/example/model_spec_spec.rb +18 -0
- data/spec/rails/example/shared_behaviour_spec.rb +16 -0
- data/spec/rails/example/test_unit_assertion_accessibility_spec.rb +33 -0
- data/spec/rails/example/view_spec_spec.rb +280 -0
- data/spec/rails/extensions/action_controller_rescue_action_spec.rb +54 -0
- data/spec/rails/extensions/action_view_base_spec.rb +48 -0
- data/spec/rails/extensions/active_record_spec.rb +14 -0
- data/spec/rails/interop/testcase_spec.rb +66 -0
- data/spec/rails/matchers/assert_select_spec.rb +814 -0
- data/spec/rails/matchers/description_generation_spec.rb +37 -0
- data/spec/rails/matchers/errors_on_spec.rb +13 -0
- data/spec/rails/matchers/have_text_spec.rb +62 -0
- data/spec/rails/matchers/include_text_spec.rb +64 -0
- data/spec/rails/matchers/redirect_to_spec.rb +209 -0
- data/spec/rails/matchers/render_template_spec.rb +176 -0
- data/spec/rails/matchers/should_change_spec.rb +15 -0
- data/spec/rails/mocks/ar_classes.rb +10 -0
- data/spec/rails/mocks/mock_model_spec.rb +106 -0
- data/spec/rails/mocks/stub_model_spec.rb +80 -0
- data/spec/rails/sample_modified_fixture.rb +8 -0
- data/spec/rails/sample_spec.rb +8 -0
- data/spec/rails/spec_server_spec.rb +96 -0
- data/spec/rails/spec_spec.rb +11 -0
- data/spec/rails_suite.rb +7 -0
- data/spec/spec_helper.rb +57 -0
- data/spec_resources/controllers/action_view_base_spec_controller.rb +2 -0
- data/spec_resources/controllers/controller_spec_controller.rb +94 -0
- data/spec_resources/controllers/redirect_spec_controller.rb +59 -0
- data/spec_resources/controllers/render_spec_controller.rb +30 -0
- data/spec_resources/controllers/rjs_spec_controller.rb +58 -0
- data/spec_resources/helpers/explicit_helper.rb +38 -0
- data/spec_resources/helpers/more_explicit_helper.rb +5 -0
- data/spec_resources/helpers/plugin_application_helper.rb +6 -0
- data/spec_resources/helpers/view_spec_helper.rb +13 -0
- data/spec_resources/views/controller_spec/_partial.rhtml +0 -0
- data/spec_resources/views/controller_spec/action_setting_flash_after_session_reset.rhtml +1 -0
- data/spec_resources/views/controller_spec/action_setting_flash_before_session_reset.rhtml +1 -0
- data/spec_resources/views/controller_spec/action_setting_the_assigns_hash.rhtml +0 -0
- data/spec_resources/views/controller_spec/action_with_errors_in_template.rhtml +1 -0
- data/spec_resources/views/controller_spec/action_with_template.rhtml +1 -0
- data/spec_resources/views/layouts/application.rhtml +0 -0
- data/spec_resources/views/layouts/simple.rhtml +0 -0
- data/spec_resources/views/objects/_object.html.erb +1 -0
- data/spec_resources/views/render_spec/_a_partial.rhtml +0 -0
- data/spec_resources/views/render_spec/action_with_alternate_layout.rhtml +0 -0
- data/spec_resources/views/render_spec/some_action.js.rjs +1 -0
- data/spec_resources/views/render_spec/some_action.rhtml +0 -0
- data/spec_resources/views/render_spec/some_action.rjs +1 -0
- data/spec_resources/views/rjs_spec/_replacement_partial.rhtml +1 -0
- data/spec_resources/views/rjs_spec/hide_div.rjs +1 -0
- data/spec_resources/views/rjs_spec/hide_page_element.rjs +1 -0
- data/spec_resources/views/rjs_spec/insert_html.rjs +1 -0
- data/spec_resources/views/rjs_spec/replace.rjs +1 -0
- data/spec_resources/views/rjs_spec/replace_html.rjs +1 -0
- data/spec_resources/views/rjs_spec/replace_html_with_partial.rjs +1 -0
- data/spec_resources/views/rjs_spec/visual_effect.rjs +1 -0
- data/spec_resources/views/rjs_spec/visual_toggle_effect.rjs +1 -0
- data/spec_resources/views/tag_spec/no_tags.rhtml +1 -0
- data/spec_resources/views/tag_spec/single_div_with_no_attributes.rhtml +1 -0
- data/spec_resources/views/tag_spec/single_div_with_one_attribute.rhtml +1 -0
- data/spec_resources/views/view_spec/_partial.rhtml +2 -0
- data/spec_resources/views/view_spec/_partial_used_twice.rhtml +0 -0
- data/spec_resources/views/view_spec/_partial_with_local_variable.rhtml +1 -0
- data/spec_resources/views/view_spec/_partial_with_sub_partial.rhtml +1 -0
- data/spec_resources/views/view_spec/_spacer.rhtml +1 -0
- data/spec_resources/views/view_spec/accessor.rhtml +5 -0
- data/spec_resources/views/view_spec/block_helper.rhtml +3 -0
- data/spec_resources/views/view_spec/entry_form.rhtml +2 -0
- data/spec_resources/views/view_spec/explicit_helper.rhtml +2 -0
- data/spec_resources/views/view_spec/foo/show.rhtml +1 -0
- data/spec_resources/views/view_spec/implicit_helper.rhtml +2 -0
- data/spec_resources/views/view_spec/multiple_helpers.rhtml +3 -0
- data/spec_resources/views/view_spec/should_not_receive.rhtml +3 -0
- data/spec_resources/views/view_spec/template_with_partial.rhtml +5 -0
- data/spec_resources/views/view_spec/template_with_partial_using_collection.rhtml +3 -0
- data/spec_resources/views/view_spec/template_with_partial_with_array.rhtml +1 -0
- data/stories/all.rb +10 -0
- data/stories/configuration/stories.rb +5 -0
- data/stories/helper.rb +6 -0
- data/stories/steps/people.rb +8 -0
- data/stories/transactions_should_rollback +15 -0
- data/stories/transactions_should_rollback.rb +25 -0
- metadata +232 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec/example/configuration'
|
2
|
+
begin
|
3
|
+
module Spec
|
4
|
+
module Example
|
5
|
+
class Configuration
|
6
|
+
# Rails 1.2.3 does a copy of the @inheritable_attributes to the subclass when the subclass is
|
7
|
+
# created. This causes an ordering issue when setting state on Configuration because the data is
|
8
|
+
# already copied.
|
9
|
+
# Iterating over EXAMPLE_GROUP_CLASSES causes the base ExampleGroup classes to have their
|
10
|
+
# @inheritable_attributes updated.
|
11
|
+
# TODO: BT - When we no longer support Rails 1.2.3, we can remove this functionality
|
12
|
+
EXAMPLE_GROUP_CLASSES = [
|
13
|
+
::Test::Unit::TestCase,
|
14
|
+
::Spec::Rails::Example::RailsExampleGroup,
|
15
|
+
::Spec::Rails::Example::FunctionalExampleGroup,
|
16
|
+
::Spec::Rails::Example::ControllerExampleGroup,
|
17
|
+
::Spec::Rails::Example::ViewExampleGroup,
|
18
|
+
::Spec::Rails::Example::HelperExampleGroup,
|
19
|
+
::Spec::Rails::Example::ModelExampleGroup
|
20
|
+
]
|
21
|
+
# All of this is ActiveRecord related and makes no sense if it's not used by the app
|
22
|
+
if defined?(ActiveRecord::Base)
|
23
|
+
def initialize
|
24
|
+
super
|
25
|
+
self.fixture_path = RAILS_ROOT + '/spec/fixtures'
|
26
|
+
end
|
27
|
+
|
28
|
+
def use_transactional_fixtures
|
29
|
+
Test::Unit::TestCase.use_transactional_fixtures
|
30
|
+
end
|
31
|
+
def use_transactional_fixtures=(value)
|
32
|
+
EXAMPLE_GROUP_CLASSES.each do |example_group|
|
33
|
+
example_group.use_transactional_fixtures = value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def use_instantiated_fixtures
|
38
|
+
Test::Unit::TestCase.use_instantiated_fixtures
|
39
|
+
end
|
40
|
+
def use_instantiated_fixtures=(value)
|
41
|
+
EXAMPLE_GROUP_CLASSES.each do |example_group|
|
42
|
+
example_group.use_instantiated_fixtures = value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def fixture_path
|
47
|
+
Test::Unit::TestCase.fixture_path
|
48
|
+
end
|
49
|
+
def fixture_path=(path)
|
50
|
+
EXAMPLE_GROUP_CLASSES.each do |example_group|
|
51
|
+
example_group.fixture_path = path
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def global_fixtures
|
56
|
+
::Test::Unit::TestCase.fixture_table_names
|
57
|
+
end
|
58
|
+
def global_fixtures=(fixtures)
|
59
|
+
EXAMPLE_GROUP_CLASSES.each do |example_group|
|
60
|
+
example_group.fixtures(*fixtures)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
rescue Exception => e
|
68
|
+
puts e.message
|
69
|
+
puts e.backtrace
|
70
|
+
raise e
|
71
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec/matchers/have'
|
2
|
+
|
3
|
+
module Spec #:nodoc:
|
4
|
+
module Matchers #:nodoc:
|
5
|
+
class Have #:nodoc:
|
6
|
+
alias_method :__original_failure_message, :failure_message
|
7
|
+
def failure_message
|
8
|
+
return "expected #{relativities[@relativity]}#{@expected} errors on :#{@args[0]}, got #{@actual}" if @collection_name == :errors_on
|
9
|
+
return "expected #{relativities[@relativity]}#{@expected} error on :#{@args[0]}, got #{@actual}" if @collection_name == :error_on
|
10
|
+
return __original_failure_message
|
11
|
+
end
|
12
|
+
|
13
|
+
alias_method :__original_description, :description
|
14
|
+
def description
|
15
|
+
return "should have #{relativities[@relativity]}#{@expected} errors on :#{@args[0]}" if @collection_name == :errors_on
|
16
|
+
return "should have #{relativities[@relativity]}#{@expected} error on :#{@args[0]}" if @collection_name == :error_on
|
17
|
+
return __original_description
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec'
|
2
|
+
|
3
|
+
require 'spec/rails/extensions/object'
|
4
|
+
|
5
|
+
require 'spec/rails/extensions/spec/example/configuration'
|
6
|
+
require 'spec/rails/extensions/spec/matchers/have'
|
7
|
+
|
8
|
+
require 'spec/rails/extensions/active_record/base'
|
9
|
+
require 'spec/rails/extensions/action_controller/base'
|
10
|
+
require 'spec/rails/extensions/action_controller/rescue'
|
11
|
+
require 'spec/rails/extensions/action_controller/test_response'
|
12
|
+
require 'spec/rails/extensions/action_view/base'
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Test
|
2
|
+
module Unit
|
3
|
+
class TestCase
|
4
|
+
# Edge rails (r8664) introduces class-wide setup & teardown callbacks for Test::Unit::TestCase.
|
5
|
+
# Make sure these still get run when running TestCases under rspec:
|
6
|
+
prepend_before(:each) do
|
7
|
+
run_callbacks :setup if respond_to?(:run_callbacks)
|
8
|
+
end
|
9
|
+
append_after(:each) do
|
10
|
+
run_callbacks :teardown if respond_to?(:run_callbacks)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
# This is a wrapper of assert_select for rspec.
|
2
|
+
|
3
|
+
module Spec # :nodoc:
|
4
|
+
module Rails
|
5
|
+
module Matchers
|
6
|
+
|
7
|
+
class AssertSelect #:nodoc:
|
8
|
+
|
9
|
+
def initialize(assertion, spec_scope, *args, &block)
|
10
|
+
@assertion = assertion
|
11
|
+
@spec_scope = spec_scope
|
12
|
+
@args = args
|
13
|
+
@block = block
|
14
|
+
end
|
15
|
+
|
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'].blank? and
|
20
|
+
response_or_text.headers['Content-Type'].to_sym == :xml
|
21
|
+
@args.unshift(HTML::Document.new(response_or_text.body, false, true).root)
|
22
|
+
elsif String === response_or_text
|
23
|
+
@args.unshift(HTML::Document.new(response_or_text).root)
|
24
|
+
end
|
25
|
+
@block = block if block
|
26
|
+
begin
|
27
|
+
@spec_scope.send(@assertion, *@args, &@block)
|
28
|
+
rescue ::Test::Unit::AssertionFailedError => @error
|
29
|
+
end
|
30
|
+
|
31
|
+
@error.nil?
|
32
|
+
end
|
33
|
+
|
34
|
+
def failure_message; @error.message; end
|
35
|
+
def negative_failure_message; "should not #{description}, but did"; end
|
36
|
+
|
37
|
+
def description
|
38
|
+
{
|
39
|
+
:assert_select => "have tag#{format_args(*@args)}",
|
40
|
+
:assert_select_email => "send email#{format_args(*@args)}",
|
41
|
+
}[@assertion]
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def format_args(*args)
|
47
|
+
return "" if args.empty?
|
48
|
+
return "(#{arg_list(*args)})"
|
49
|
+
end
|
50
|
+
|
51
|
+
def arg_list(*args)
|
52
|
+
args.collect do |arg|
|
53
|
+
arg.respond_to?(:description) ? arg.description : arg.inspect
|
54
|
+
end.join(", ")
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
# :call-seq:
|
60
|
+
# response.should have_tag(*args, &block)
|
61
|
+
# string.should have_tag(*args, &block)
|
62
|
+
#
|
63
|
+
# wrapper for assert_select with additional support for using
|
64
|
+
# css selectors to set expectation on Strings. Use this in
|
65
|
+
# helper specs, for example, to set expectations on the results
|
66
|
+
# of helper methods.
|
67
|
+
#
|
68
|
+
# == Examples
|
69
|
+
#
|
70
|
+
# # in a controller spec
|
71
|
+
# response.should have_tag("div", "some text")
|
72
|
+
#
|
73
|
+
# # in a helper spec (person_address_tag is a method in the helper)
|
74
|
+
# person_address_tag.should have_tag("input#person_address")
|
75
|
+
#
|
76
|
+
# see documentation for assert_select at http://api.rubyonrails.org/
|
77
|
+
def have_tag(*args, &block)
|
78
|
+
AssertSelect.new(:assert_select, self, *args, &block)
|
79
|
+
end
|
80
|
+
|
81
|
+
# wrapper for a nested assert_select
|
82
|
+
#
|
83
|
+
# response.should have_tag("div#form") do
|
84
|
+
# with_tag("input#person_name[name=?]", "person[name]")
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# see documentation for assert_select at http://api.rubyonrails.org/
|
88
|
+
def with_tag(*args, &block)
|
89
|
+
should have_tag(*args, &block)
|
90
|
+
end
|
91
|
+
|
92
|
+
# wrapper for a nested assert_select with false
|
93
|
+
#
|
94
|
+
# response.should have_tag("div#1") do
|
95
|
+
# without_tag("span", "some text that shouldn't be there")
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# see documentation for assert_select at http://api.rubyonrails.org/
|
99
|
+
def without_tag(*args, &block)
|
100
|
+
should_not have_tag(*args, &block)
|
101
|
+
end
|
102
|
+
|
103
|
+
# :call-seq:
|
104
|
+
# response.should have_rjs(*args, &block)
|
105
|
+
#
|
106
|
+
# wrapper for assert_select_rjs
|
107
|
+
#
|
108
|
+
# see documentation for assert_select_rjs at http://api.rubyonrails.org/
|
109
|
+
def have_rjs(*args, &block)
|
110
|
+
AssertSelect.new(:assert_select_rjs, self, *args, &block)
|
111
|
+
end
|
112
|
+
|
113
|
+
# :call-seq:
|
114
|
+
# response.should send_email(*args, &block)
|
115
|
+
#
|
116
|
+
# wrapper for assert_select_email
|
117
|
+
#
|
118
|
+
# see documentation for assert_select_email at http://api.rubyonrails.org/
|
119
|
+
def send_email(*args, &block)
|
120
|
+
AssertSelect.new(:assert_select_email, self, *args, &block)
|
121
|
+
end
|
122
|
+
|
123
|
+
# wrapper for assert_select_encoded
|
124
|
+
#
|
125
|
+
# see documentation for assert_select_encoded at http://api.rubyonrails.org/
|
126
|
+
def with_encoded(*args, &block)
|
127
|
+
should AssertSelect.new(:assert_select_encoded, self, *args, &block)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Spec
|
2
|
+
module Matchers
|
3
|
+
class Change
|
4
|
+
def evaluate_value_proc_with_ensured_evaluation_of_proxy
|
5
|
+
value = evaluate_value_proc_without_ensured_evaluation_of_proxy
|
6
|
+
ActiveRecord::Associations::AssociationProxy === value ? value.dup : value
|
7
|
+
end
|
8
|
+
alias_method_chain :evaluate_value_proc, :ensured_evaluation_of_proxy
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Spec
|
2
|
+
module Rails
|
3
|
+
module Matchers
|
4
|
+
|
5
|
+
class HaveText #:nodoc:
|
6
|
+
|
7
|
+
def initialize(expected)
|
8
|
+
@expected = expected
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(response_or_text)
|
12
|
+
@actual = response_or_text.respond_to?(:body) ? response_or_text.body : response_or_text
|
13
|
+
return actual =~ expected if Regexp === expected
|
14
|
+
return actual == expected unless Regexp === expected
|
15
|
+
end
|
16
|
+
|
17
|
+
def failure_message
|
18
|
+
"expected #{expected.inspect}, got #{actual.inspect}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def negative_failure_message
|
22
|
+
"expected not to have text #{expected.inspect}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
"have text #{expected.inspect}"
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
attr_reader :expected
|
31
|
+
attr_reader :actual
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
# :call-seq:
|
36
|
+
# response.should have_text(expected)
|
37
|
+
# response.should_not have_text(expected)
|
38
|
+
#
|
39
|
+
# Accepts a String or a Regexp, matching a String using ==
|
40
|
+
# and a Regexp using =~.
|
41
|
+
#
|
42
|
+
# If response_or_text has a #body, then that is used as to match against
|
43
|
+
# else it uses response_or_text
|
44
|
+
#
|
45
|
+
# Use this instead of <tt>response.should have_tag()</tt>
|
46
|
+
# when you want to match the whole string or whole body
|
47
|
+
#
|
48
|
+
# == Examples
|
49
|
+
#
|
50
|
+
# response.should have_text("This is the expected text")
|
51
|
+
def have_text(text)
|
52
|
+
HaveText.new(text)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Spec
|
2
|
+
module Rails
|
3
|
+
module Matchers
|
4
|
+
|
5
|
+
class IncludeText #:nodoc:
|
6
|
+
|
7
|
+
def initialize(expected)
|
8
|
+
@expected = expected
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(response_or_text)
|
12
|
+
@actual = response_or_text.respond_to?(:body) ? response_or_text.body : response_or_text
|
13
|
+
return actual.include?(expected)
|
14
|
+
end
|
15
|
+
|
16
|
+
def failure_message
|
17
|
+
"expected to find #{expected.inspect} in #{actual.inspect}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def negative_failure_message
|
21
|
+
"expected not to include text #{expected.inspect}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
"include text #{expected.inspect}"
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
attr_reader :expected
|
30
|
+
attr_reader :actual
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# :call-seq:
|
36
|
+
# response.should include_text(expected)
|
37
|
+
# response.should_not include_text(expected)
|
38
|
+
#
|
39
|
+
# Accepts a String, matching using include?
|
40
|
+
#
|
41
|
+
# Use this instead of <tt>response.should have_text()</tt>
|
42
|
+
# when you either don't know or don't care where on the page
|
43
|
+
# this text appears.
|
44
|
+
#
|
45
|
+
# == Examples
|
46
|
+
#
|
47
|
+
# response.should include_text("This text will be in the actual string")
|
48
|
+
def include_text(text)
|
49
|
+
IncludeText.new(text)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module Spec
|
2
|
+
module Rails
|
3
|
+
module Matchers
|
4
|
+
|
5
|
+
class RedirectTo #:nodoc:
|
6
|
+
|
7
|
+
def initialize(request, expected)
|
8
|
+
@expected = expected
|
9
|
+
@request = request
|
10
|
+
end
|
11
|
+
|
12
|
+
def matches?(response)
|
13
|
+
@redirected = response.redirect?
|
14
|
+
@actual = response.redirect_url
|
15
|
+
return false unless @redirected
|
16
|
+
if @expected.instance_of? Hash
|
17
|
+
return false unless @actual =~ %r{^\w+://#{@request.host}}
|
18
|
+
return false unless actual_redirect_to_valid_route
|
19
|
+
return actual_hash == expected_hash
|
20
|
+
else
|
21
|
+
return @actual == expected_url
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def actual_hash
|
26
|
+
hash_from_url @actual
|
27
|
+
end
|
28
|
+
|
29
|
+
def expected_hash
|
30
|
+
hash_from_url expected_url
|
31
|
+
end
|
32
|
+
|
33
|
+
def actual_redirect_to_valid_route
|
34
|
+
actual_hash
|
35
|
+
end
|
36
|
+
|
37
|
+
def hash_from_url(url)
|
38
|
+
query_hash(url).merge(path_hash(url)).with_indifferent_access
|
39
|
+
end
|
40
|
+
|
41
|
+
def path_hash(url)
|
42
|
+
path = url.sub(%r{^\w+://#{@request.host}(?::\d+)?}, "").split("?", 2)[0]
|
43
|
+
ActionController::Routing::Routes.recognize_path path
|
44
|
+
end
|
45
|
+
|
46
|
+
def query_hash(url)
|
47
|
+
query = url.split("?", 2)[1] || ""
|
48
|
+
QueryParameterParser.parse_query_parameters(query, @request)
|
49
|
+
end
|
50
|
+
|
51
|
+
def expected_url
|
52
|
+
case @expected
|
53
|
+
when Hash
|
54
|
+
return ActionController::UrlRewriter.new(@request, {}).rewrite(@expected)
|
55
|
+
when :back
|
56
|
+
return @request.env['HTTP_REFERER']
|
57
|
+
when %r{^\w+://.*}
|
58
|
+
return @expected
|
59
|
+
else
|
60
|
+
return "http://#{@request.host}" + (@expected.split('')[0] == '/' ? '' : '/') + @expected
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def failure_message
|
65
|
+
if @redirected
|
66
|
+
return %Q{expected redirect to #{@expected.inspect}, got redirect to #{@actual.inspect}}
|
67
|
+
else
|
68
|
+
return %Q{expected redirect to #{@expected.inspect}, got no redirect}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def negative_failure_message
|
73
|
+
return %Q{expected not to be redirected to #{@expected.inspect}, but was} if @redirected
|
74
|
+
end
|
75
|
+
|
76
|
+
def description
|
77
|
+
"redirect to #{@actual.inspect}"
|
78
|
+
end
|
79
|
+
|
80
|
+
class QueryParameterParser
|
81
|
+
def self.parse_query_parameters(query, request)
|
82
|
+
if defined?(CGIMethods)
|
83
|
+
CGIMethods.parse_query_parameters(query)
|
84
|
+
else
|
85
|
+
request.class.parse_query_parameters(query)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# :call-seq:
|
92
|
+
# response.should redirect_to(url)
|
93
|
+
# response.should redirect_to(:action => action_name)
|
94
|
+
# response.should redirect_to(:controller => controller_name, :action => action_name)
|
95
|
+
# response.should_not redirect_to(url)
|
96
|
+
# response.should_not redirect_to(:action => action_name)
|
97
|
+
# response.should_not redirect_to(:controller => controller_name, :action => action_name)
|
98
|
+
#
|
99
|
+
# Passes if the response is a redirect to the url, action or controller/action.
|
100
|
+
# Useful in controller specs (integration or isolation mode).
|
101
|
+
#
|
102
|
+
# == Examples
|
103
|
+
#
|
104
|
+
# response.should redirect_to("path/to/action")
|
105
|
+
# response.should redirect_to("http://test.host/path/to/action")
|
106
|
+
# response.should redirect_to(:action => 'list')
|
107
|
+
def redirect_to(opts)
|
108
|
+
RedirectTo.new(request, opts)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Spec
|
2
|
+
module Rails
|
3
|
+
module Matchers
|
4
|
+
|
5
|
+
class RenderTemplate #:nodoc:
|
6
|
+
|
7
|
+
def initialize(expected, controller)
|
8
|
+
@controller = controller
|
9
|
+
@expected = expected
|
10
|
+
end
|
11
|
+
|
12
|
+
def matches?(response)
|
13
|
+
|
14
|
+
if response.respond_to?(:rendered_file)
|
15
|
+
@actual = response.rendered_file
|
16
|
+
else
|
17
|
+
@actual = response.rendered_template.to_s
|
18
|
+
end
|
19
|
+
return false if @actual.blank?
|
20
|
+
given_controller_path, given_file = path_and_file(@actual)
|
21
|
+
expected_controller_path, expected_file = path_and_file(@expected)
|
22
|
+
given_controller_path == expected_controller_path && given_file.match(expected_file)
|
23
|
+
end
|
24
|
+
|
25
|
+
def failure_message
|
26
|
+
"expected #{@expected.inspect}, got #{@actual.inspect}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def negative_failure_message
|
30
|
+
"expected not to render #{@expected.inspect}, but did"
|
31
|
+
end
|
32
|
+
|
33
|
+
def description
|
34
|
+
"render template #{@expected.inspect}"
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def path_and_file(path)
|
39
|
+
parts = path.split('/')
|
40
|
+
file = parts.pop
|
41
|
+
controller = parts.empty? ? current_controller_path : parts.join('/')
|
42
|
+
return controller, file
|
43
|
+
end
|
44
|
+
|
45
|
+
def controller_path_from(path)
|
46
|
+
parts = path.split('/')
|
47
|
+
parts.pop
|
48
|
+
parts.join('/')
|
49
|
+
end
|
50
|
+
|
51
|
+
def current_controller_path
|
52
|
+
@controller.class.to_s.underscore.gsub(/_controller$/,'')
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
# :call-seq:
|
58
|
+
# response.should render_template(path)
|
59
|
+
# response.should_not render_template(path)
|
60
|
+
#
|
61
|
+
# Passes if the specified template is rendered by the response.
|
62
|
+
# Useful in controller specs (integration or isolation mode).
|
63
|
+
#
|
64
|
+
# <code>path</code> can include the controller path or not. It
|
65
|
+
# can also include an optional extension (no extension assumes .rhtml).
|
66
|
+
#
|
67
|
+
# Note that partials must be spelled with the preceding underscore.
|
68
|
+
#
|
69
|
+
# == Examples
|
70
|
+
#
|
71
|
+
# response.should render_template('list')
|
72
|
+
# response.should render_template('same_controller/list')
|
73
|
+
# response.should render_template('other_controller/list')
|
74
|
+
#
|
75
|
+
# #rjs
|
76
|
+
# response.should render_template('list.rjs')
|
77
|
+
# response.should render_template('same_controller/list.rjs')
|
78
|
+
# response.should render_template('other_controller/list.rjs')
|
79
|
+
#
|
80
|
+
# #partials
|
81
|
+
# response.should render_template('_a_partial')
|
82
|
+
# response.should render_template('same_controller/_a_partial')
|
83
|
+
# response.should render_template('other_controller/_a_partial')
|
84
|
+
def render_template(path)
|
85
|
+
RenderTemplate.new(path.to_s, @controller)
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
dir = File.dirname(__FILE__)
|
2
|
+
require 'spec/rails/matchers/assert_select'
|
3
|
+
require 'spec/rails/matchers/change'
|
4
|
+
require 'spec/rails/matchers/have_text'
|
5
|
+
require 'spec/rails/matchers/include_text'
|
6
|
+
require 'spec/rails/matchers/redirect_to'
|
7
|
+
require 'spec/rails/matchers/render_template'
|
8
|
+
|
9
|
+
module Spec
|
10
|
+
module Rails
|
11
|
+
# Spec::Rails::Expectations::Matchers provides several expectation matchers
|
12
|
+
# intended to work with Rails components like models and responses. For example:
|
13
|
+
#
|
14
|
+
# response.should redirect_to("some/url") #redirect_to(url) is the matcher.
|
15
|
+
#
|
16
|
+
# In addition to those you see below, the arbitrary predicate feature of RSpec
|
17
|
+
# makes the following available as well:
|
18
|
+
#
|
19
|
+
# response.should be_success #passes if response.success?
|
20
|
+
# response.should be_redirect #passes if response.redirect?
|
21
|
+
#
|
22
|
+
# Note that many of these matchers are part of a wrapper of <tt>assert_select</tt>, so
|
23
|
+
# the documentation comes straight from that with some slight modifications.
|
24
|
+
# <tt>assert_select</tt> is a Test::Unit extension originally contributed to the
|
25
|
+
# Rails community as a plugin by Assaf Arkin and eventually shipped as part of Rails.
|
26
|
+
#
|
27
|
+
# For more info on <tt>assert_select</tt>, see the relevant Rails documentation.
|
28
|
+
module Matchers
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|