rspec-rails-w-factory_girl 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +7 -0
- data/Contribute.rdoc +4 -0
- data/History.rdoc +310 -0
- data/License.txt +33 -0
- data/Manifest.txt +166 -0
- data/README.rdoc +45 -0
- data/Rakefile +72 -0
- data/TODO.txt +17 -0
- data/Upgrade.rdoc +148 -0
- data/generators/integration_spec/integration_spec_generator.rb +10 -0
- data/generators/integration_spec/templates/integration_spec.rb +4 -0
- data/generators/rspec/CHANGES +1 -0
- data/generators/rspec/rspec_generator.rb +73 -0
- data/generators/rspec/templates/previous_failures.txt +0 -0
- data/generators/rspec/templates/rcov.opts +2 -0
- data/generators/rspec/templates/rspec.rake +144 -0
- data/generators/rspec/templates/script/autospec +6 -0
- data/generators/rspec/templates/script/spec +10 -0
- data/generators/rspec/templates/spec.opts +4 -0
- data/generators/rspec/templates/spec_helper.rb +54 -0
- data/generators/rspec_controller/USAGE +33 -0
- data/generators/rspec_controller/rspec_controller_generator.rb +47 -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 +28 -0
- data/generators/rspec_model/USAGE +18 -0
- data/generators/rspec_model/rspec_model_generator.rb +34 -0
- data/generators/rspec_model/templates/factories.rb +8 -0
- data/generators/rspec_model/templates/model_spec.rb +13 -0
- data/generators/rspec_scaffold/rspec_scaffold_generator.rb +154 -0
- data/generators/rspec_scaffold/templates/controller_spec.rb +131 -0
- data/generators/rspec_scaffold/templates/edit_erb_spec.rb +25 -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 +25 -0
- data/generators/rspec_scaffold/templates/routing_spec.rb +33 -0
- data/generators/rspec_scaffold/templates/show_erb_spec.rb +22 -0
- data/init.rb +9 -0
- data/lib/autotest/discover.rb +5 -0
- data/lib/autotest/rails_rspec.rb +76 -0
- data/lib/spec/rails/example/assigns_hash_proxy.rb +39 -0
- data/lib/spec/rails/example/controller_example_group.rb +285 -0
- data/lib/spec/rails/example/cookies_proxy.rb +29 -0
- data/lib/spec/rails/example/functional_example_group.rb +106 -0
- data/lib/spec/rails/example/helper_example_group.rb +153 -0
- data/lib/spec/rails/example/integration_example_group.rb +16 -0
- data/lib/spec/rails/example/model_example_group.rb +15 -0
- data/lib/spec/rails/example/render_observer.rb +80 -0
- data/lib/spec/rails/example/routing_example_group.rb +13 -0
- data/lib/spec/rails/example/routing_helpers.rb +66 -0
- data/lib/spec/rails/example/view_example_group.rb +199 -0
- data/lib/spec/rails/example.rb +48 -0
- data/lib/spec/rails/extensions/action_controller/rescue.rb +42 -0
- data/lib/spec/rails/extensions/action_controller/test_case.rb +16 -0
- data/lib/spec/rails/extensions/action_controller/test_response.rb +21 -0
- data/lib/spec/rails/extensions/action_view/base.rb +35 -0
- data/lib/spec/rails/extensions/active_record/base.rb +45 -0
- data/lib/spec/rails/extensions/active_support/test_case.rb +7 -0
- data/lib/spec/rails/extensions/spec/matchers/have.rb +23 -0
- data/lib/spec/rails/extensions/spec/runner/configuration.rb +44 -0
- data/lib/spec/rails/extensions.rb +11 -0
- data/lib/spec/rails/interop/testcase.rb +14 -0
- data/lib/spec/rails/matchers/ar_be_valid.rb +27 -0
- data/lib/spec/rails/matchers/assert_select.rb +180 -0
- data/lib/spec/rails/matchers/change.rb +13 -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 +126 -0
- data/lib/spec/rails/matchers/render_template.rb +129 -0
- data/lib/spec/rails/matchers/route_to.rb +149 -0
- data/lib/spec/rails/matchers.rb +32 -0
- data/lib/spec/rails/mocks.rb +136 -0
- data/lib/spec/rails/version.rb +16 -0
- data/lib/spec/rails.rb +26 -0
- data/spec/autotest/mappings_spec.rb +86 -0
- data/spec/rails_suite.rb +7 -0
- data/spec/resources/controllers/action_view_base_spec_controller.rb +2 -0
- data/spec/resources/controllers/application.rb +9 -0
- data/spec/resources/controllers/controller_spec_controller.rb +127 -0
- data/spec/resources/controllers/example.txt +1 -0
- data/spec/resources/controllers/redirect_spec_controller.rb +70 -0
- data/spec/resources/controllers/render_spec_controller.rb +34 -0
- data/spec/resources/controllers/rjs_spec_controller.rb +58 -0
- data/spec/resources/helpers/addition_helper.rb +5 -0
- data/spec/resources/helpers/explicit_helper.rb +46 -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/models/animal.rb +4 -0
- data/spec/resources/models/person.rb +18 -0
- data/spec/resources/models/thing.rb +3 -0
- data/spec/resources/views/controller_spec/_partial.html.erb +0 -0
- data/spec/resources/views/controller_spec/action_setting_flash_after_session_reset.html.erb +1 -0
- data/spec/resources/views/controller_spec/action_setting_flash_before_session_reset.html.erb +1 -0
- data/spec/resources/views/controller_spec/action_setting_the_assigns_hash.html.erb +0 -0
- data/spec/resources/views/controller_spec/action_with_errors_in_template.html.erb +1 -0
- data/spec/resources/views/controller_spec/action_with_template.html.erb +1 -0
- data/spec/resources/views/layouts/application.html.erb +0 -0
- data/spec/resources/views/layouts/simple.html.erb +0 -0
- data/spec/resources/views/objects/_object.html.erb +1 -0
- data/spec/resources/views/render_spec/_a_partial.html.erb +0 -0
- data/spec/resources/views/render_spec/action_with_alternate_layout.html.erb +0 -0
- data/spec/resources/views/render_spec/some_action.html.erb +0 -0
- data/spec/resources/views/render_spec/some_action.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/_replacement_partial.html.erb +1 -0
- data/spec/resources/views/rjs_spec/hide_div.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/hide_page_element.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/insert_html.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/replace.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/replace_html.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/replace_html_with_partial.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/visual_effect.js.rjs +1 -0
- data/spec/resources/views/rjs_spec/visual_toggle_effect.js.rjs +1 -0
- data/spec/resources/views/tag_spec/no_tags.html.erb +1 -0
- data/spec/resources/views/tag_spec/single_div_with_no_attributes.html.erb +1 -0
- data/spec/resources/views/tag_spec/single_div_with_one_attribute.html.erb +1 -0
- data/spec/resources/views/view_spec/_partial.html.erb +2 -0
- data/spec/resources/views/view_spec/_partial_used_twice.html.erb +0 -0
- data/spec/resources/views/view_spec/_partial_with_local_variable.html.erb +1 -0
- data/spec/resources/views/view_spec/_partial_with_sub_partial.html.erb +1 -0
- data/spec/resources/views/view_spec/_spacer.html.erb +1 -0
- data/spec/resources/views/view_spec/accessor.html.erb +6 -0
- data/spec/resources/views/view_spec/block_helper.html.erb +3 -0
- data/spec/resources/views/view_spec/entry_form.html.erb +2 -0
- data/spec/resources/views/view_spec/explicit_helper.html.erb +2 -0
- data/spec/resources/views/view_spec/foo/show.html.erb +1 -0
- data/spec/resources/views/view_spec/implicit_helper.html.erb +2 -0
- data/spec/resources/views/view_spec/multiple_helpers.html.erb +3 -0
- data/spec/resources/views/view_spec/path_params.html.erb +1 -0
- data/spec/resources/views/view_spec/should_not_receive.html.erb +3 -0
- data/spec/resources/views/view_spec/template_with_partial.html.erb +5 -0
- data/spec/resources/views/view_spec/template_with_partial_using_collection.html.erb +3 -0
- data/spec/resources/views/view_spec/template_with_partial_with_array.html.erb +1 -0
- data/spec/resources/views/view_spec/view_helpers.html.erb +1 -0
- data/spec/spec/rails/example/assigns_hash_proxy_spec.rb +109 -0
- data/spec/spec/rails/example/configuration_spec.rb +65 -0
- data/spec/spec/rails/example/controller_example_group_spec.rb +307 -0
- data/spec/spec/rails/example/controller_isolation_spec.rb +75 -0
- data/spec/spec/rails/example/cookies_proxy_spec.rb +87 -0
- data/spec/spec/rails/example/error_handling_spec.rb +90 -0
- data/spec/spec/rails/example/example_group_factory_spec.rb +112 -0
- data/spec/spec/rails/example/helper_example_group_spec.rb +247 -0
- data/spec/spec/rails/example/model_example_group_spec.rb +32 -0
- data/spec/spec/rails/example/routing_example_group_spec.rb +10 -0
- data/spec/spec/rails/example/shared_routing_example_group_examples.rb +237 -0
- data/spec/spec/rails/example/test_unit_assertion_accessibility_spec.rb +33 -0
- data/spec/spec/rails/example/view_example_group_spec.rb +346 -0
- data/spec/spec/rails/extensions/action_view_base_spec.rb +79 -0
- data/spec/spec/rails/extensions/active_record_spec.rb +14 -0
- data/spec/spec/rails/interop/testcase_spec.rb +70 -0
- data/spec/spec/rails/matchers/ar_be_valid_spec.rb +19 -0
- data/spec/spec/rails/matchers/assert_select_spec.rb +835 -0
- data/spec/spec/rails/matchers/errors_on_spec.rb +37 -0
- data/spec/spec/rails/matchers/have_text_spec.rb +69 -0
- data/spec/spec/rails/matchers/include_text_spec.rb +62 -0
- data/spec/spec/rails/matchers/redirect_to_spec.rb +253 -0
- data/spec/spec/rails/matchers/render_template_spec.rb +208 -0
- data/spec/spec/rails/matchers/should_change_spec.rb +15 -0
- data/spec/spec/rails/mocks/ar_classes.rb +10 -0
- data/spec/spec/rails/mocks/mock_model_spec.rb +109 -0
- data/spec/spec/rails/mocks/stub_model_spec.rb +80 -0
- data/spec/spec/rails/sample_modified_fixture.rb +8 -0
- data/spec/spec/rails/sample_spec.rb +8 -0
- data/spec/spec/rails/spec_spec.rb +11 -0
- data/spec/spec_helper.rb +78 -0
- metadata +301 -0
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
|
3
|
+
module Spec
|
4
|
+
module Rails
|
5
|
+
module Matchers
|
6
|
+
USAGE = ArgumentError.new( 'usage: { :method => "path" }.should route_to( :controller => "controller", :action => "action", [ args ] )' )
|
7
|
+
|
8
|
+
class PathDecomposer
|
9
|
+
def self.decompose_path(path)
|
10
|
+
method, path = if Hash === path
|
11
|
+
raise USAGE if path.keys.size > 1
|
12
|
+
path.entries.first
|
13
|
+
else
|
14
|
+
[:get, path]
|
15
|
+
end
|
16
|
+
path, querystring = path.split('?')
|
17
|
+
return method, path, querystring
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class RouteTo #:nodoc:
|
22
|
+
def initialize(expected, example)
|
23
|
+
@route, @example = expected,example
|
24
|
+
end
|
25
|
+
|
26
|
+
def matches?(path)
|
27
|
+
begin
|
28
|
+
@actual = path
|
29
|
+
method, path, querystring = PathDecomposer.decompose_path(path)
|
30
|
+
params = querystring.blank? ? {} : Rack::Utils.parse_query(querystring).symbolize_keys!
|
31
|
+
@example.assert_routing({ :method => method, :path => path }, @route, {}, params)
|
32
|
+
true
|
33
|
+
rescue ActionController::RoutingError, ::Test::Unit::AssertionFailedError, ActionController::MethodNotAllowed => e
|
34
|
+
raise e.class, "#{e}\nIf you're expecting this failure, we suggest { :#{method} => \"#{path}\" }.should_not be_routable"
|
35
|
+
rescue Exception => e
|
36
|
+
raise e.class, "#{e}\n#{e.backtrace.join( "\n" )}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def does_not_match(path)
|
41
|
+
raise ArgumentError, "Don't test a negative route like this."
|
42
|
+
end
|
43
|
+
|
44
|
+
def failure_message_for_should
|
45
|
+
"Expected #{@expected.inspect} to route to #{@actual.inspect}, but it didn't.\n"+
|
46
|
+
"In this case, we expected you to get an exception. So this message probably means something weird happened."
|
47
|
+
end
|
48
|
+
|
49
|
+
def failure_message_for_should_not
|
50
|
+
"Expected a routing error, but the route passed instead. \nNote, when expecting routes to fail, you should use 'should_not be_routable' instead."
|
51
|
+
end
|
52
|
+
|
53
|
+
def description
|
54
|
+
"route to #{@expected.inspect}"
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
attr_reader :expected
|
59
|
+
attr_reader :actual
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
# :call-seq:
|
64
|
+
# "path".should route_to(expected) # assumes GET
|
65
|
+
# { :get => "path" }.should route_to(expected)
|
66
|
+
# { :put => "path" }.should route_to(expected)
|
67
|
+
#
|
68
|
+
# Uses ActionController::Routing::Routes to verify that
|
69
|
+
# the path-and-method routes to a given set of options.
|
70
|
+
# Also verifies route-generation, so that the expected options
|
71
|
+
# do generate a pathname consisten with the indicated path/method.
|
72
|
+
#
|
73
|
+
# For negative tests, only the route recognition failure can be
|
74
|
+
# tested; since route generation via path_to() will always generate
|
75
|
+
# a path as requested. Use .should_not be_routable() in this case.
|
76
|
+
#
|
77
|
+
# == Examples
|
78
|
+
# { :get => '/registrations/1/edit' }.
|
79
|
+
# should route_to(:controller => 'registrations', :action => 'edit', :id => '1')
|
80
|
+
# { :put => "/registrations/1" }.should
|
81
|
+
# route_to(:controller => 'registrations', :action => 'update', :id => 1)
|
82
|
+
# { :post => "/registrations/" }.should
|
83
|
+
# route_to(:controller => 'registrations', :action => 'create')
|
84
|
+
|
85
|
+
def route_to(expected)
|
86
|
+
RouteTo.new(expected, self)
|
87
|
+
end
|
88
|
+
|
89
|
+
class BeRoutable
|
90
|
+
def initialize(example)
|
91
|
+
@example = example
|
92
|
+
end
|
93
|
+
|
94
|
+
def matches?(path)
|
95
|
+
begin
|
96
|
+
@actual = path
|
97
|
+
method, path = PathDecomposer.decompose_path(path)
|
98
|
+
@example.assert_recognizes({}, { :method => method, :path => path }, {} )
|
99
|
+
true
|
100
|
+
rescue ActionController::RoutingError, ActionController::MethodNotAllowed
|
101
|
+
false
|
102
|
+
rescue ::Test::Unit::AssertionFailedError => e
|
103
|
+
# the second thingy will always be "<{}>" becaues of the way we called assert_recognizes({}...) above.
|
104
|
+
e.to_s =~ /<(.*)> did not match <\{\}>/ and @actual_place = $1 or raise
|
105
|
+
true
|
106
|
+
end
|
107
|
+
end
|
108
|
+
def failure_message_for_should
|
109
|
+
"Expected '#{@actual.keys.first.to_s.upcase} #{@actual.values.first}' to be routable, but it wasn't.\n"+
|
110
|
+
"To really test routability, we recommend #{@actual.inspect}.\n"+
|
111
|
+
" should route_to( :action => 'action', :controller => 'controller' )\n\n"+
|
112
|
+
|
113
|
+
"That way, you'll verify where your route goes to. Plus, we'll verify\n"+
|
114
|
+
"the generation of the expected path from the action/controller, as in\n"+
|
115
|
+
"the url_for() helper."
|
116
|
+
end
|
117
|
+
|
118
|
+
def failure_message_for_should_not
|
119
|
+
"Expected '#{@actual.keys.first.to_s.upcase} #{@actual.values.first}' to fail, but it routed to #{@actual_place} instead"
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
# :call-seq:
|
124
|
+
# { "path" }.should_not be_routable # assumes GET
|
125
|
+
# { :get => "path" }.should_not be_routable
|
126
|
+
# { :put => "path" }.should_not be_routable
|
127
|
+
#
|
128
|
+
# Uses ActionController::Routing::Routes to verify that
|
129
|
+
# the path-and-method cannot be routed to a controller.
|
130
|
+
# Since url_for() will always generate a path, even if that
|
131
|
+
# path is not routable, the negative test only needs to be
|
132
|
+
# performed on the route recognition.
|
133
|
+
#
|
134
|
+
# Don't use this matcher for testing expected routability -
|
135
|
+
# use .should route_to( :controller => "controller", :action => "action" ) instead
|
136
|
+
#
|
137
|
+
# == Examples
|
138
|
+
# { :get => '/registrations/1/attendees/3/edit' }.should_not be_routable
|
139
|
+
# { :get => '/attendees/3/edit' }.should route_to( ...<controller/action>... )
|
140
|
+
|
141
|
+
def be_routable
|
142
|
+
BeRoutable.new(self)
|
143
|
+
end
|
144
|
+
|
145
|
+
alias_method :be_routeable, :be_routable
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Spec
|
2
|
+
module Rails
|
3
|
+
# Spec::Rails::Expectations::Matchers provides several expectation matchers
|
4
|
+
# intended to work with Rails components like models and responses. For example:
|
5
|
+
#
|
6
|
+
# response.should redirect_to("some/url") #redirect_to(url) is the matcher.
|
7
|
+
#
|
8
|
+
# In addition to those you see below, the arbitrary predicate feature of RSpec
|
9
|
+
# makes the following available as well:
|
10
|
+
#
|
11
|
+
# response.should be_success #passes if response.success?
|
12
|
+
# response.should be_redirect #passes if response.redirect?
|
13
|
+
#
|
14
|
+
# Note that many of these matchers are part of a wrapper of <tt>assert_select</tt>, so
|
15
|
+
# the documentation comes straight from that with some slight modifications.
|
16
|
+
# <tt>assert_select</tt> is a Test::Unit extension originally contributed to the
|
17
|
+
# Rails community as a plugin by Assaf Arkin and eventually shipped as part of Rails.
|
18
|
+
#
|
19
|
+
# For more info on <tt>assert_select</tt>, see the relevant Rails documentation.
|
20
|
+
module Matchers
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'spec/rails/matchers/ar_be_valid'
|
26
|
+
require 'spec/rails/matchers/assert_select'
|
27
|
+
require 'spec/rails/matchers/change'
|
28
|
+
require 'spec/rails/matchers/have_text'
|
29
|
+
require 'spec/rails/matchers/include_text'
|
30
|
+
require 'spec/rails/matchers/redirect_to'
|
31
|
+
require 'spec/rails/matchers/route_to'
|
32
|
+
require 'spec/rails/matchers/render_template'
|
@@ -0,0 +1,136 @@
|
|
1
|
+
module Spec
|
2
|
+
module Rails
|
3
|
+
|
4
|
+
class IllegalDataAccessException < StandardError; end
|
5
|
+
|
6
|
+
module Mocks
|
7
|
+
|
8
|
+
# Creates a mock object instance for a +model_class+ with common
|
9
|
+
# methods stubbed out. Additional methods may be easily stubbed (via
|
10
|
+
# add_stubs) if +stubs+ is passed.
|
11
|
+
def mock_model(model_class, options_and_stubs = {})
|
12
|
+
id = options_and_stubs[:id] || next_id
|
13
|
+
options_and_stubs = options_and_stubs.reverse_merge({
|
14
|
+
:id => id,
|
15
|
+
:to_param => id.to_s,
|
16
|
+
:new_record? => false,
|
17
|
+
:destroyed? => false,
|
18
|
+
:errors => stub("errors", :count => 0)
|
19
|
+
})
|
20
|
+
m = mock("#{model_class.name}_#{id}", options_and_stubs)
|
21
|
+
m.__send__(:__mock_proxy).instance_eval <<-CODE
|
22
|
+
def @target.as_new_record
|
23
|
+
self.stub!(:id).and_return nil
|
24
|
+
self.stub!(:to_param).and_return nil
|
25
|
+
self.stub!(:new_record?).and_return true
|
26
|
+
self
|
27
|
+
end
|
28
|
+
def @target.is_a?(other)
|
29
|
+
#{model_class}.ancestors.include?(other)
|
30
|
+
end
|
31
|
+
def @target.kind_of?(other)
|
32
|
+
#{model_class}.ancestors.include?(other)
|
33
|
+
end
|
34
|
+
def @target.instance_of?(other)
|
35
|
+
other == #{model_class}
|
36
|
+
end
|
37
|
+
def @target.class
|
38
|
+
#{model_class}
|
39
|
+
end
|
40
|
+
CODE
|
41
|
+
yield m if block_given?
|
42
|
+
m
|
43
|
+
end
|
44
|
+
|
45
|
+
module ModelStubber
|
46
|
+
def connection
|
47
|
+
raise Spec::Rails::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
|
48
|
+
end
|
49
|
+
def new_record?
|
50
|
+
id.nil?
|
51
|
+
end
|
52
|
+
def as_new_record
|
53
|
+
self.id = nil
|
54
|
+
self
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# :call-seq:
|
59
|
+
# stub_model(Model)
|
60
|
+
# stub_model(Model).as_new_record
|
61
|
+
# stub_model(Model, hash_of_stubs)
|
62
|
+
# stub_model(Model, instance_variable_name, hash_of_stubs)
|
63
|
+
#
|
64
|
+
# Creates an instance of +Model+ that is prohibited from accessing the
|
65
|
+
# database*. For each key in +hash_of_stubs+, if the model has a
|
66
|
+
# matching attribute (determined by asking it) are simply assigned the
|
67
|
+
# submitted values. If the model does not have a matching attribute, the
|
68
|
+
# key/value pair is assigned as a stub return value using RSpec's
|
69
|
+
# mocking/stubbing framework.
|
70
|
+
#
|
71
|
+
# <tt>new_record?</tt> is overridden to return the result of id.nil?
|
72
|
+
# This means that by default new_record? will return false. If you want
|
73
|
+
# the object to behave as a new record, sending it +as_new_record+ will
|
74
|
+
# set the id to nil. You can also explicitly set :id => nil, in which
|
75
|
+
# case new_record? will return true, but using +as_new_record+ makes the
|
76
|
+
# example a bit more descriptive.
|
77
|
+
#
|
78
|
+
# While you can use stub_model in any example (model, view, controller,
|
79
|
+
# helper), it is especially useful in view examples, which are
|
80
|
+
# inherently more state-based than interaction-based.
|
81
|
+
#
|
82
|
+
# == Database Independence
|
83
|
+
#
|
84
|
+
# +stub_model+ does not make your examples entirely
|
85
|
+
# database-independent. It does not stop the model class itself from
|
86
|
+
# loading up its columns from the database. It just prevents data access
|
87
|
+
# from the object itself. To completely decouple from the database, take
|
88
|
+
# a look at libraries like unit_record or NullDB.
|
89
|
+
#
|
90
|
+
# == Examples
|
91
|
+
#
|
92
|
+
# stub_model(Person)
|
93
|
+
# stub_model(Person).as_new_record
|
94
|
+
# stub_model(Person, :id => 37)
|
95
|
+
# stub_model(Person) do |person|
|
96
|
+
# person.first_name = "David"
|
97
|
+
# end
|
98
|
+
def stub_model(model_class, stubs={})
|
99
|
+
stubs = {:id => next_id}.merge(stubs)
|
100
|
+
returning model_class.new do |model|
|
101
|
+
model.id = stubs.delete(:id)
|
102
|
+
model.extend ModelStubber
|
103
|
+
stubs.each do |k,v|
|
104
|
+
if model.has_attribute?(k)
|
105
|
+
model[k] = stubs.delete(k)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
model.stub!(stubs)
|
109
|
+
yield model if block_given?
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# DEPRECATED - use object.stub!(:method => value, :method2 => value)
|
114
|
+
#
|
115
|
+
# Stubs methods on +object+ (if +object+ is a symbol or string a new mock
|
116
|
+
# with that name will be created). +stubs+ is a Hash of +method=>value+
|
117
|
+
def add_stubs(object, stubs = {}) #:nodoc:
|
118
|
+
Kernel.warn <<-WARNING
|
119
|
+
DEPRECATION NOTICE: add_stubs is deprecated and will be removed
|
120
|
+
from a future version of rspec-rails. Use this instead:
|
121
|
+
|
122
|
+
object.stub!(:method => value, :method2 => value)
|
123
|
+
|
124
|
+
WARNING
|
125
|
+
object.stub!(stubs)
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
@@model_id = 1000
|
130
|
+
def next_id
|
131
|
+
@@model_id += 1
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Spec # :nodoc:
|
2
|
+
module Rails # :nodoc:
|
3
|
+
module VERSION # :nodoc:
|
4
|
+
unless defined? MAJOR
|
5
|
+
MAJOR = 1
|
6
|
+
MINOR = 3
|
7
|
+
TINY = 2
|
8
|
+
PRE = nil
|
9
|
+
|
10
|
+
STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
|
11
|
+
|
12
|
+
SUMMARY = "rspec-rails #{STRING}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/spec/rails.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
begin
|
2
|
+
require_dependency 'application_controller'
|
3
|
+
rescue MissingSourceFile
|
4
|
+
require_dependency 'application'
|
5
|
+
end
|
6
|
+
require 'rack/utils'
|
7
|
+
|
8
|
+
require 'action_controller/test_process'
|
9
|
+
require 'action_controller/integration'
|
10
|
+
require 'active_support/test_case'
|
11
|
+
require 'active_record/fixtures' if defined?(ActiveRecord::Base)
|
12
|
+
|
13
|
+
require 'spec/test/unit'
|
14
|
+
|
15
|
+
require 'spec/rails/matchers'
|
16
|
+
require 'spec/rails/mocks'
|
17
|
+
require 'spec/rails/example'
|
18
|
+
require 'spec/rails/extensions'
|
19
|
+
require 'spec/rails/interop/testcase'
|
20
|
+
|
21
|
+
Spec::Example::ExampleGroupFactory.default(ActiveSupport::TestCase)
|
22
|
+
|
23
|
+
if ActionView::Base.respond_to?(:cache_template_extensions)
|
24
|
+
ActionView::Base.cache_template_extensions = false
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require File.dirname(__FILE__) + '/../../lib/autotest/rails_rspec'
|
3
|
+
require File.dirname(__FILE__) + '/../../../rspec/spec/autotest/autotest_matchers'
|
4
|
+
|
5
|
+
describe Autotest::RailsRspec, "file mapping" do
|
6
|
+
before(:each) do
|
7
|
+
@autotest = Autotest::RailsRspec.new
|
8
|
+
@autotest.hook :initialize
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should map model example to model" do
|
12
|
+
@autotest.should map_specs(['spec/models/thing_spec.rb']).
|
13
|
+
to('app/models/thing.rb')
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should map controller example to controller" do
|
17
|
+
@autotest.should map_specs(['spec/controllers/things_controller_spec.rb']).
|
18
|
+
to('app/controllers/things_controller.rb')
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should map view.rhtml" do
|
22
|
+
@autotest.should map_specs(['spec/views/things/index.rhtml_spec.rb']).
|
23
|
+
to('app/views/things/index.rhtml')
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should map view.rhtml with underscores in example filename" do
|
27
|
+
@autotest.should map_specs(['spec/views/things/index_rhtml_spec.rb']).
|
28
|
+
to('app/views/things/index.rhtml')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should map view.html.erb" do
|
32
|
+
@autotest.should map_specs(['spec/views/things/index.html.erb_spec.rb']).
|
33
|
+
to('app/views/things/index.html.erb')
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "between routes.rb and things which depend on routes" do
|
37
|
+
it "should map routes.rb to controllers" do
|
38
|
+
@autotest.should map_specs(['spec/controllers/things_controller_spec.rb']).
|
39
|
+
to('config/routes.rb')
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should map routes.rb to views" do
|
43
|
+
@autotest.should map_specs(['spec/views/things/action.html.erb_spec.rb']).
|
44
|
+
to('config/routes.rb')
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should map routes.rb to helpers" do
|
48
|
+
@autotest.should map_specs(['spec/helpers/things_helper_spec.rb']).
|
49
|
+
to('config/routes.rb')
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should map routing example to routes" do
|
53
|
+
@autotest.should map_specs(['spec/routing/thing_spec.rb']).
|
54
|
+
to('config/routes.rb')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "between the config and specs" do
|
59
|
+
['spec/controllers/things_controller_spec.rb',
|
60
|
+
'spec/views/things/action.html.erb_spec.rb',
|
61
|
+
'spec/helpers/things_helper_spec.rb',
|
62
|
+
'spec/routing/thing_spec.rb',
|
63
|
+
'spec/models/thing_spec.rb'].each do |file_path|
|
64
|
+
|
65
|
+
it "should map environment.rb to #{file_path}" do
|
66
|
+
@autotest.should map_specs([file_path]).
|
67
|
+
to('config/environment.rb')
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should map environments/test.rb to #{file_path}" do
|
71
|
+
@autotest.should map_specs([file_path]).
|
72
|
+
to('config/environments/test.rb')
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should map boot.rb to #{file_path}" do
|
76
|
+
@autotest.should map_specs([file_path]).
|
77
|
+
to('config/boot.rb')
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should map spec_helper.rb to #{file_path}" do
|
81
|
+
@autotest.should map_specs([file_path]).
|
82
|
+
to('spec/spec_helper.rb')
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/spec/rails_suite.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
class ControllerSpecController < ActionController::Base
|
2
|
+
before_filter :raise_error, :only => :action_with_skipped_before_filter
|
3
|
+
|
4
|
+
def raise_error
|
5
|
+
raise "from a before filter"
|
6
|
+
end
|
7
|
+
|
8
|
+
skip_before_filter :raise_error
|
9
|
+
|
10
|
+
prepend_view_path File.join(File.dirname(__FILE__), "..", "views")
|
11
|
+
|
12
|
+
def some_action
|
13
|
+
render :template => "template/that/does/not/actually/exist"
|
14
|
+
end
|
15
|
+
|
16
|
+
def some_action_with_implied_template
|
17
|
+
end
|
18
|
+
|
19
|
+
def action_with_template
|
20
|
+
render :template => "controller_spec/action_with_template"
|
21
|
+
end
|
22
|
+
|
23
|
+
def action_which_sets_flash
|
24
|
+
flash[:flash_key] = "flash value"
|
25
|
+
render :text => ""
|
26
|
+
end
|
27
|
+
|
28
|
+
def action_which_gets_session
|
29
|
+
raise "expected #{params[:session_key].inspect}\ngot #{session[:session_key].inspect}" unless (session[:session_key] == params[:expected])
|
30
|
+
render :text => ""
|
31
|
+
end
|
32
|
+
|
33
|
+
def action_which_sets_session
|
34
|
+
session[:session_key] = "session value"
|
35
|
+
end
|
36
|
+
|
37
|
+
def action_which_gets_cookie
|
38
|
+
raise "expected #{params[:expected].inspect}, got #{cookies[:cookie_key].inspect}" unless (cookies[:cookie_key] == params[:expected])
|
39
|
+
render :text => ""
|
40
|
+
end
|
41
|
+
|
42
|
+
def action_which_sets_cookie
|
43
|
+
cookies['cookie_key'] = params[:value]
|
44
|
+
render :text => ""
|
45
|
+
end
|
46
|
+
|
47
|
+
def action_with_partial
|
48
|
+
render :partial => "controller_spec/partial"
|
49
|
+
end
|
50
|
+
|
51
|
+
def action_with_partial_with_object
|
52
|
+
render :partial => "controller_spec/partial", :object => params[:thing]
|
53
|
+
end
|
54
|
+
|
55
|
+
def action_with_partial_with_locals
|
56
|
+
render :partial => "controller_spec/partial", :locals => {:thing => params[:thing]}
|
57
|
+
end
|
58
|
+
|
59
|
+
def action_with_errors_in_template
|
60
|
+
render :template => "controller_spec/action_with_errors_in_template"
|
61
|
+
end
|
62
|
+
|
63
|
+
def action_setting_the_assigns_hash
|
64
|
+
@indirect_assigns_key = :indirect_assigns_key_value
|
65
|
+
end
|
66
|
+
|
67
|
+
def action_setting_flash_after_session_reset
|
68
|
+
reset_session
|
69
|
+
flash[:after_reset] = "available"
|
70
|
+
end
|
71
|
+
|
72
|
+
def action_setting_flash_before_session_reset
|
73
|
+
flash[:before_reset] = 'available'
|
74
|
+
reset_session
|
75
|
+
end
|
76
|
+
|
77
|
+
def action_with_render_update
|
78
|
+
render :update do |page|
|
79
|
+
page.replace :bottom, 'replace_me',
|
80
|
+
:partial => 'non_existent_partial'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def action_with_skipped_before_filter
|
85
|
+
render :text => ""
|
86
|
+
end
|
87
|
+
|
88
|
+
def action_that_renders_inline
|
89
|
+
render :inline => "<%= 'inline code' %>"
|
90
|
+
end
|
91
|
+
|
92
|
+
def action_that_assigns_false_to_a_variable
|
93
|
+
@a_variable = false
|
94
|
+
render :text => ""
|
95
|
+
end
|
96
|
+
|
97
|
+
def action_with_two_arg_render
|
98
|
+
render :update, :status => 404 do |page|
|
99
|
+
page.visual_effect :highlight, 'user_list'
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class RescuedError < Exception; end
|
104
|
+
class UnRescuedError < Exception; end
|
105
|
+
|
106
|
+
rescue_from RescuedError do |e|
|
107
|
+
render :text => 'Rescued!'
|
108
|
+
end
|
109
|
+
|
110
|
+
def rescued_error_action
|
111
|
+
raise ControllerSpecController::RescuedError
|
112
|
+
end
|
113
|
+
|
114
|
+
def un_rescued_error_action
|
115
|
+
raise ControllerSpecController::UnRescuedError
|
116
|
+
end
|
117
|
+
|
118
|
+
def action_that_returns_headers
|
119
|
+
render :text => request.headers[params[:header]]
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class ControllerInheritingFromApplicationControllerController < ApplicationController
|
124
|
+
def action_with_inherited_before_filter
|
125
|
+
render :text => ""
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
This is an example file to download.
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class RedirectSpecController < ApplicationController
|
2
|
+
|
3
|
+
def action_with_no_redirect
|
4
|
+
render :text => "this is just here to keep this from causing a MissingTemplate error"
|
5
|
+
end
|
6
|
+
|
7
|
+
def action_with_redirect_to_somewhere
|
8
|
+
redirect_to :action => 'somewhere'
|
9
|
+
end
|
10
|
+
|
11
|
+
def action_with_redirect_to_other_somewhere
|
12
|
+
redirect_to :controller => 'render_spec', :action => 'text_action'
|
13
|
+
end
|
14
|
+
|
15
|
+
def action_with_redirect_to_somewhere_and_return
|
16
|
+
redirect_to :action => 'somewhere' and return
|
17
|
+
render :text => "this is after the return"
|
18
|
+
end
|
19
|
+
|
20
|
+
def somewhere
|
21
|
+
render :text => "this is just here to keep this from causing a MissingTemplate error"
|
22
|
+
end
|
23
|
+
|
24
|
+
def action_with_redirect_to_rspec_site
|
25
|
+
redirect_to "http://rspec.rubyforge.org"
|
26
|
+
end
|
27
|
+
|
28
|
+
def action_with_redirect_back
|
29
|
+
redirect_to :back
|
30
|
+
end
|
31
|
+
|
32
|
+
def action_with_redirect_in_respond_to
|
33
|
+
respond_to do |wants|
|
34
|
+
wants.html { redirect_to :action => 'somewhere' }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def action_with_redirect_which_creates_query_string
|
39
|
+
redirect_to :action => "somewhere", :id => 1111, :param1 => "value1", :param2 => "value2"
|
40
|
+
end
|
41
|
+
|
42
|
+
# note: sometimes this is the URL which rails will generate from the hash in
|
43
|
+
# action_with_redirect_which_creates_query_string
|
44
|
+
def action_with_redirect_with_query_string_order1
|
45
|
+
redirect_to "http://test.host/redirect_spec/somewhere/1111?param1=value1¶m2=value2"
|
46
|
+
end
|
47
|
+
|
48
|
+
# note: sometimes this is the URL which rails will generate from the hash in
|
49
|
+
# action_with_redirect_which_creates_query_string
|
50
|
+
def action_with_redirect_with_query_string_order2
|
51
|
+
redirect_to "http://test.host/redirect_spec/somewhere/1111?param2=value2¶m1=value1"
|
52
|
+
end
|
53
|
+
|
54
|
+
def action_with_redirect_to_unroutable_url_inside_app
|
55
|
+
redirect_to :controller => "nonexistant", :action => "none"
|
56
|
+
end
|
57
|
+
|
58
|
+
def action_with_method_restriction
|
59
|
+
render :text => ''
|
60
|
+
end
|
61
|
+
|
62
|
+
def action_to_redirect_to_action_with_method_restriction
|
63
|
+
redirect_to :action => 'action_with_method_restriction'
|
64
|
+
end
|
65
|
+
|
66
|
+
def action_with_redirect_to_somewhere_with_status
|
67
|
+
redirect_to :action => 'somewhere', :status => 301
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|