rspec-rails 2.99.0 → 3.0.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- checksums.yaml.gz.sig +2 -0
- data.tar.gz.sig +0 -0
- data/Changelog.md +40 -68
- data/License.txt +1 -0
- data/README.md +6 -4
- data/features/Generators.md +24 -0
- data/features/GettingStarted.md +84 -0
- data/features/README.md +56 -0
- data/features/RailsVersions.md +4 -0
- data/features/Transactions.md +84 -0
- data/features/Upgrade.md +121 -0
- data/features/controller_specs/Cookies.md +57 -0
- data/features/controller_specs/README.md +45 -0
- data/features/controller_specs/anonymous_controller.feature +378 -0
- data/features/controller_specs/bypass_rescue.feature +75 -0
- data/features/controller_specs/controller_spec.feature +58 -0
- data/features/controller_specs/engine_routes.feature +51 -0
- data/features/controller_specs/isolation_from_views.feature +87 -0
- data/features/controller_specs/render_views.feature +114 -0
- data/features/directory_structure.feature +71 -0
- data/features/feature_specs/feature_spec.feature +35 -0
- data/features/helper_specs/helper_spec.feature +122 -0
- data/features/mailer_specs/url_helpers.feature +38 -0
- data/features/matchers/README.md +18 -0
- data/features/matchers/new_record_matcher.feature +41 -0
- data/features/matchers/redirect_to_matcher.feature +40 -0
- data/features/matchers/relation_match_array.feature +27 -0
- data/features/matchers/render_template_matcher.feature +49 -0
- data/features/mocks/mock_model.feature +147 -0
- data/features/mocks/stub_model.feature +58 -0
- data/features/model_specs/README.md +21 -0
- data/features/model_specs/errors_on.feature +51 -0
- data/features/model_specs/records.feature +27 -0
- data/features/model_specs/transactional_examples.feature +109 -0
- data/features/request_specs/request_spec.feature +49 -0
- data/features/routing_specs/README.md +16 -0
- data/features/routing_specs/be_routable_matcher.feature +80 -0
- data/features/routing_specs/engine_routes.feature +38 -0
- data/features/routing_specs/named_routes.feature +18 -0
- data/features/routing_specs/route_to_matcher.feature +90 -0
- data/features/step_definitions/additional_cli_steps.rb +4 -0
- data/features/step_definitions/model_steps.rb +3 -0
- data/features/support/capybara.rb +7 -0
- data/features/support/env.rb +53 -0
- data/features/support/rails_versions.rb +4 -0
- data/features/support/rubinius.rb +6 -0
- data/features/view_specs/inferred_controller_path.feature +45 -0
- data/features/view_specs/stub_template.feature +51 -0
- data/features/view_specs/view_spec.feature +206 -0
- data/lib/generators/rspec/controller/templates/controller_spec.rb +1 -1
- data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +2 -21
- data/lib/generators/rspec/integration/integration_generator.rb +2 -3
- data/lib/generators/rspec/integration/templates/request_spec.rb +1 -1
- data/lib/generators/rspec/mailer/templates/mailer_spec.rb +4 -4
- data/lib/generators/rspec/scaffold/scaffold_generator.rb +2 -3
- data/lib/generators/rspec/scaffold/templates/controller_spec.rb +16 -16
- data/lib/generators/rspec/scaffold/templates/edit_spec.rb +2 -2
- data/lib/generators/rspec/scaffold/templates/index_spec.rb +1 -1
- data/lib/generators/rspec/scaffold/templates/new_spec.rb +2 -2
- data/lib/generators/rspec/scaffold/templates/routing_spec.rb +7 -7
- data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
- data/lib/rspec/rails.rb +0 -5
- data/lib/rspec/rails/adapters.rb +5 -12
- data/lib/rspec/rails/example.rb +55 -24
- data/lib/rspec/rails/example/controller_example_group.rb +7 -31
- data/lib/rspec/rails/example/view_example_group.rb +0 -3
- data/lib/rspec/rails/matchers.rb +1 -2
- data/lib/rspec/rails/matchers/be_a_new.rb +1 -1
- data/lib/rspec/rails/matchers/be_new_record.rb +1 -1
- data/lib/rspec/rails/matchers/be_valid.rb +1 -1
- data/lib/rspec/rails/matchers/have_rendered.rb +1 -1
- data/lib/rspec/rails/matchers/redirect_to.rb +1 -1
- data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
- data/lib/rspec/rails/matchers/routing_matchers.rb +6 -10
- data/lib/rspec/rails/mocks.rb +5 -41
- data/lib/rspec/rails/tasks/rspec.rake +5 -12
- data/lib/rspec/rails/vendor/capybara.rb +4 -35
- data/lib/rspec/rails/version.rb +1 -1
- data/spec/generators/rspec/controller/controller_generator_spec.rb +97 -0
- data/spec/generators/rspec/helper/helper_generator_spec.rb +30 -0
- data/spec/generators/rspec/install/install_generator_spec.rb +30 -0
- data/spec/generators/rspec/integration/integration_generator_spec.rb +44 -0
- data/spec/generators/rspec/mailer/mailer_generator_spec.rb +48 -0
- data/spec/generators/rspec/model/model_generator_spec.rb +52 -0
- data/spec/generators/rspec/observer/observer_generator_spec.rb +21 -0
- data/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +138 -0
- data/spec/generators/rspec/view/view_generator_spec.rb +41 -0
- data/spec/rspec/rails/assertion_adapter_spec.rb +28 -0
- data/spec/rspec/rails/assertion_delegator_spec.rb +43 -0
- data/spec/rspec/rails/configuration_spec.rb +26 -0
- data/spec/rspec/rails/deprecations_spec.rb +18 -0
- data/spec/rspec/rails/example/controller_example_group_spec.rb +100 -0
- data/spec/rspec/rails/example/feature_example_group_spec.rb +56 -0
- data/spec/rspec/rails/example/helper_example_group_spec.rb +66 -0
- data/spec/rspec/rails/example/mailer_example_group_spec.rb +21 -0
- data/spec/rspec/rails/example/model_example_group_spec.rb +15 -0
- data/spec/rspec/rails/example/request_example_group_spec.rb +17 -0
- data/spec/rspec/rails/example/routing_example_group_spec.rb +32 -0
- data/spec/rspec/rails/example/view_example_group_spec.rb +220 -0
- data/spec/rspec/rails/extensions/active_model/errors_on_spec.rb +23 -0
- data/spec/rspec/rails/extensions/active_record/base_spec.rb +42 -0
- data/spec/rspec/rails/fixture_support_spec.rb +17 -0
- data/spec/rspec/rails/matchers/be_a_new_spec.rb +142 -0
- data/spec/rspec/rails/matchers/be_new_record_spec.rb +33 -0
- data/spec/rspec/rails/matchers/be_routable_spec.rb +41 -0
- data/spec/rspec/rails/matchers/be_valid_spec.rb +44 -0
- data/spec/rspec/rails/matchers/has_spec.rb +29 -0
- data/spec/rspec/rails/matchers/have_rendered_spec.rb +93 -0
- data/spec/rspec/rails/matchers/redirect_to_spec.rb +80 -0
- data/spec/rspec/rails/matchers/relation_match_array_spec.rb +31 -0
- data/spec/rspec/rails/matchers/route_to_spec.rb +151 -0
- data/spec/rspec/rails/minitest_lifecycle_adapter_spec.rb +22 -0
- data/spec/rspec/rails/mocks/mock_model_spec.rb +378 -0
- data/spec/rspec/rails/mocks/stub_model_spec.rb +154 -0
- data/spec/rspec/rails/setup_and_teardown_adapter_spec.rb +32 -0
- data/spec/rspec/rails/view_rendering_spec.rb +111 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/support/ar_classes.rb +42 -0
- data/spec/support/helpers.rb +20 -0
- data/spec/support/matchers.rb +9 -0
- data/spec/support/null_object.rb +6 -0
- metadata +229 -42
- metadata.gz.sig +0 -0
- data/lib/autotest/rails_rspec2.rb +0 -91
- data/lib/rspec/rails/infer_type_configuration.rb +0 -26
- data/lib/rspec/rails/matchers/have_extension.rb +0 -36
- data/lib/rspec/rails/module_inclusion.rb +0 -19
@@ -0,0 +1,75 @@
|
|
1
|
+
Feature: bypass rescue
|
2
|
+
|
3
|
+
Use `bypass_rescue` to bypass both Rails' default handling of errors in
|
4
|
+
controller actions, and any custom handling declared with a `rescue_from`
|
5
|
+
statement.
|
6
|
+
|
7
|
+
This lets you specify details of the exception being raised, regardless of
|
8
|
+
how it might be handled upstream.
|
9
|
+
|
10
|
+
Background:
|
11
|
+
Given a file named "spec/controllers/gadgets_controller_spec_context.rb" with:
|
12
|
+
"""ruby
|
13
|
+
class AccessDenied < StandardError; end
|
14
|
+
|
15
|
+
class ApplicationController < ActionController::Base
|
16
|
+
rescue_from AccessDenied, :with => :access_denied
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def access_denied
|
21
|
+
redirect_to "/401.html"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
"""
|
25
|
+
|
26
|
+
Scenario: standard exception handling using `rescue_from`
|
27
|
+
Given a file named "spec/controllers/gadgets_controller_spec.rb" with:
|
28
|
+
"""ruby
|
29
|
+
require "spec_helper"
|
30
|
+
|
31
|
+
require 'controllers/gadgets_controller_spec_context'
|
32
|
+
|
33
|
+
describe GadgetsController do
|
34
|
+
before do
|
35
|
+
def controller.index
|
36
|
+
raise AccessDenied
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "index" do
|
41
|
+
it "redirects to the /401.html page" do
|
42
|
+
get :index
|
43
|
+
expect(response).to redirect_to("/401.html")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
"""
|
48
|
+
When I run `rspec spec/controllers/gadgets_controller_spec.rb`
|
49
|
+
Then the examples should all pass
|
50
|
+
|
51
|
+
Scenario: bypass `rescue_from` handling with `bypass_rescue`
|
52
|
+
Given a file named "spec/controllers/gadgets_controller_spec.rb" with:
|
53
|
+
"""ruby
|
54
|
+
require "spec_helper"
|
55
|
+
|
56
|
+
require 'controllers/gadgets_controller_spec_context'
|
57
|
+
|
58
|
+
describe GadgetsController do
|
59
|
+
before do
|
60
|
+
def controller.index
|
61
|
+
raise AccessDenied
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "index" do
|
66
|
+
it "raises AccessDenied" do
|
67
|
+
bypass_rescue
|
68
|
+
expect { get :index }.to raise_error(AccessDenied)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
"""
|
73
|
+
When I run `rspec spec/controllers/gadgets_controller_spec.rb`
|
74
|
+
Then the examples should all pass
|
75
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
Feature: controller spec
|
2
|
+
|
3
|
+
Scenario: simple passing example
|
4
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
5
|
+
"""ruby
|
6
|
+
require "spec_helper"
|
7
|
+
|
8
|
+
describe WidgetsController do
|
9
|
+
describe "GET index" do
|
10
|
+
it "has a 200 status code" do
|
11
|
+
get :index
|
12
|
+
expect(response.status).to eq(200)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
"""
|
17
|
+
When I run `rspec spec`
|
18
|
+
Then the example should pass
|
19
|
+
|
20
|
+
Scenario: controller is exposed to global before hooks
|
21
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
22
|
+
"""ruby
|
23
|
+
require "spec_helper"
|
24
|
+
|
25
|
+
RSpec.configure {|c| c.before { expect(controller).not_to be_nil }}
|
26
|
+
|
27
|
+
describe WidgetsController do
|
28
|
+
describe "GET index" do
|
29
|
+
it "doesn't matter" do
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
"""
|
34
|
+
When I run `rspec spec`
|
35
|
+
Then the example should pass
|
36
|
+
|
37
|
+
Scenario: controller is extended with a helper module
|
38
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
39
|
+
"""ruby
|
40
|
+
require "spec_helper"
|
41
|
+
|
42
|
+
module MyHelper
|
43
|
+
def my_variable
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
RSpec.configure {|c| c.include MyHelper }
|
48
|
+
|
49
|
+
describe WidgetsController do
|
50
|
+
let(:my_variable) { 'is a value' }
|
51
|
+
|
52
|
+
describe 'something' do
|
53
|
+
specify { expect(my_variable).to eq 'is a value' }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
"""
|
57
|
+
When I run `rspec spec`
|
58
|
+
Then the example should pass
|
@@ -0,0 +1,51 @@
|
|
1
|
+
Feature: engine routes for controllers
|
2
|
+
|
3
|
+
Controller specs can specify the routeset that will be used for the example
|
4
|
+
group. This is most useful when testing Rails engines.
|
5
|
+
|
6
|
+
@unsupported-on-rails-3-0
|
7
|
+
Scenario: specify engine route
|
8
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
9
|
+
"""ruby
|
10
|
+
require "spec_helper"
|
11
|
+
|
12
|
+
# A very simple Rails engine
|
13
|
+
module MyEngine
|
14
|
+
class Engine < ::Rails::Engine
|
15
|
+
isolate_namespace MyEngine
|
16
|
+
end
|
17
|
+
|
18
|
+
Engine.routes.draw do
|
19
|
+
resources :widgets, :only => [:show] do
|
20
|
+
get :random, :on => :collection
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class WidgetsController < ::ActionController::Base
|
25
|
+
def random
|
26
|
+
@random_widget = Widget.all.shuffle.first
|
27
|
+
redirect_to widget_path(@random_widget)
|
28
|
+
end
|
29
|
+
|
30
|
+
def show
|
31
|
+
@widget = Widget.find(params[:id])
|
32
|
+
render :text => @widget.name
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe MyEngine::WidgetsController do
|
38
|
+
routes { MyEngine::Engine.routes }
|
39
|
+
|
40
|
+
it "redirects to a random widget" do
|
41
|
+
widget1 = Widget.create!(:name => "Widget 1")
|
42
|
+
widget2 = Widget.create!(:name => "Widget 2")
|
43
|
+
|
44
|
+
get :random
|
45
|
+
expect(response).to be_redirect
|
46
|
+
expect(response).to redirect_to(assigns(:random_widget))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
"""
|
50
|
+
When I run `rspec spec`
|
51
|
+
Then the examples should all pass
|
@@ -0,0 +1,87 @@
|
|
1
|
+
Feature: views are stubbed by default
|
2
|
+
|
3
|
+
By default, controller specs stub views with a template that renders an empty
|
4
|
+
string instead of the views in the app. This allows you specify which view
|
5
|
+
template an action should try to render regardless of whether the template
|
6
|
+
compiles cleanly.
|
7
|
+
|
8
|
+
NOTE: unlike rspec-rails-1.x, the real template must exist.
|
9
|
+
|
10
|
+
Scenario: expect template that is rendered by controller action (passes)
|
11
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
12
|
+
"""ruby
|
13
|
+
require "spec_helper"
|
14
|
+
|
15
|
+
describe WidgetsController do
|
16
|
+
describe "index" do
|
17
|
+
it "renders the index template" do
|
18
|
+
get :index
|
19
|
+
expect(response).to render_template("index")
|
20
|
+
expect(response.body).to eq ""
|
21
|
+
end
|
22
|
+
it "renders the widgets/index template" do
|
23
|
+
get :index
|
24
|
+
expect(response).to render_template("widgets/index")
|
25
|
+
expect(response.body).to eq ""
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
"""
|
30
|
+
When I run `rspec spec`
|
31
|
+
Then the examples should all pass
|
32
|
+
|
33
|
+
Scenario: expect template that is not rendered by controller action (fails)
|
34
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
35
|
+
"""ruby
|
36
|
+
require "spec_helper"
|
37
|
+
|
38
|
+
describe WidgetsController do
|
39
|
+
describe "index" do
|
40
|
+
it "renders the 'new' template" do
|
41
|
+
get :index
|
42
|
+
expect(response).to render_template("new")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
"""
|
47
|
+
When I run `rspec spec`
|
48
|
+
Then the output should contain "1 example, 1 failure"
|
49
|
+
|
50
|
+
Scenario: expect empty templates to render when view path is changed at runtime (passes)
|
51
|
+
Given a file named "spec/controllers/things_controller_spec.rb" with:
|
52
|
+
"""ruby
|
53
|
+
require "spec_helper"
|
54
|
+
|
55
|
+
describe ThingsController do
|
56
|
+
describe "custom_action" do
|
57
|
+
it "renders an empty custom_action template" do
|
58
|
+
controller.prepend_view_path 'app/views'
|
59
|
+
controller.append_view_path 'app/views'
|
60
|
+
get :custom_action
|
61
|
+
expect(response).to render_template("custom_action")
|
62
|
+
expect(response.body).to eq ""
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
"""
|
67
|
+
When I run `rspec spec`
|
68
|
+
Then the examples should all pass
|
69
|
+
|
70
|
+
Scenario: expect template to render the real template with render_views when view path is changed at runtime
|
71
|
+
Given a file named "spec/controllers/things_controller_spec.rb" with:
|
72
|
+
"""ruby
|
73
|
+
require "spec_helper"
|
74
|
+
|
75
|
+
describe ThingsController do
|
76
|
+
render_views
|
77
|
+
|
78
|
+
it "renders the real custom_action template" do
|
79
|
+
controller.prepend_view_path 'app/views'
|
80
|
+
get :custom_action
|
81
|
+
expect(response).to render_template("custom_action")
|
82
|
+
expect(response.body).to match(/template for a custom action/)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
"""
|
86
|
+
When I run `rspec spec`
|
87
|
+
Then the examples should all pass
|
@@ -0,0 +1,114 @@
|
|
1
|
+
Feature: render_views
|
2
|
+
|
3
|
+
You can tell a controller example group to render views with the
|
4
|
+
`render_views` declaration in any individual group, or globally.
|
5
|
+
|
6
|
+
Scenario: render_views directly in a single group
|
7
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
8
|
+
"""ruby
|
9
|
+
require "spec_helper"
|
10
|
+
|
11
|
+
describe WidgetsController do
|
12
|
+
render_views
|
13
|
+
|
14
|
+
describe "GET index" do
|
15
|
+
it "says 'Listing widgets'" do
|
16
|
+
get :index
|
17
|
+
expect(response.body).to match /Listing widgets/m
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
"""
|
22
|
+
When I run `rspec spec`
|
23
|
+
Then the examples should all pass
|
24
|
+
|
25
|
+
Scenario: render_views on and off in nested groups
|
26
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
27
|
+
"""ruby
|
28
|
+
require "spec_helper"
|
29
|
+
|
30
|
+
describe WidgetsController do
|
31
|
+
context "with render_views" do
|
32
|
+
render_views
|
33
|
+
|
34
|
+
describe "GET index" do
|
35
|
+
it "renders the actual template" do
|
36
|
+
get :index
|
37
|
+
expect(response.body).to match /Listing widgets/m
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with render_views(false) nested in a group with render_views" do
|
42
|
+
render_views false
|
43
|
+
|
44
|
+
describe "GET index" do
|
45
|
+
it "renders the RSpec generated template" do
|
46
|
+
get :index
|
47
|
+
expect(response.body).to eq("")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "without render_views" do
|
54
|
+
describe "GET index" do
|
55
|
+
it "renders the RSpec generated template" do
|
56
|
+
get :index
|
57
|
+
expect(response.body).to eq("")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "with render_views again" do
|
63
|
+
render_views
|
64
|
+
|
65
|
+
describe "GET index" do
|
66
|
+
it "renders the actual template" do
|
67
|
+
get :index
|
68
|
+
expect(response.body).to match /Listing widgets/m
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
"""
|
74
|
+
When I run `rspec spec --order default --format documentation`
|
75
|
+
Then the output should contain:
|
76
|
+
"""ruby
|
77
|
+
WidgetsController
|
78
|
+
with render_views
|
79
|
+
GET index
|
80
|
+
renders the actual template
|
81
|
+
with render_views(false) nested in a group with render_views
|
82
|
+
GET index
|
83
|
+
renders the RSpec generated template
|
84
|
+
without render_views
|
85
|
+
GET index
|
86
|
+
renders the RSpec generated template
|
87
|
+
with render_views again
|
88
|
+
GET index
|
89
|
+
renders the actual template
|
90
|
+
"""
|
91
|
+
|
92
|
+
Scenario: render_views globally
|
93
|
+
Given a file named "spec/support/render_views.rb" with:
|
94
|
+
"""ruby
|
95
|
+
RSpec.configure do |config|
|
96
|
+
config.render_views
|
97
|
+
end
|
98
|
+
"""
|
99
|
+
And a file named "spec/controllers/widgets_controller_spec.rb" with:
|
100
|
+
"""ruby
|
101
|
+
require "spec_helper"
|
102
|
+
|
103
|
+
describe WidgetsController do
|
104
|
+
describe "GET index" do
|
105
|
+
it "renders the index template" do
|
106
|
+
get :index
|
107
|
+
expect(response.body).to match /Listing widgets/m
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
"""
|
112
|
+
When I run `rspec spec`
|
113
|
+
Then the examples should all pass
|
114
|
+
|
@@ -0,0 +1,71 @@
|
|
1
|
+
Feature: Directory Structure
|
2
|
+
|
3
|
+
Specs are usually placed in a canonical directory structure that describes
|
4
|
+
their purpose.
|
5
|
+
|
6
|
+
* Model specs reside in the `spec/models` directory
|
7
|
+
* Controller specs reside in the `spec/controllers` directory
|
8
|
+
* Request specs reside in the `spec/requests` directory
|
9
|
+
* Feature specs reside in the `spec/features` directory
|
10
|
+
* View specs reside in the `spec/views` directory
|
11
|
+
* Helper specs reside in the `spec/helpers` directory
|
12
|
+
* Mailer specs reside in the `spec/mailers` directory
|
13
|
+
* Routing specs reside in the `spec/routing` directory
|
14
|
+
|
15
|
+
If you follow this directory structure, RSpec will automatically include the
|
16
|
+
correct test support functions for each type of test.
|
17
|
+
|
18
|
+
Application developers are free to use a different directory structure, but
|
19
|
+
will need to specify the types manually by adding a `:type` metadata key (for
|
20
|
+
example, `describe WidgetsController, :type => :controller`)
|
21
|
+
|
22
|
+
Scenario: Specs in the `spec/controllers` directory automatically tagged as controller specs
|
23
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
24
|
+
"""ruby
|
25
|
+
require "spec_helper"
|
26
|
+
|
27
|
+
describe WidgetsController do
|
28
|
+
it "responds successfully" do
|
29
|
+
get :index
|
30
|
+
expect(response.status).to eq(200)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
"""
|
34
|
+
When I run `rspec spec`
|
35
|
+
Then the example should pass
|
36
|
+
|
37
|
+
Scenario: Specs in other directories must have their types specified manually
|
38
|
+
Given a file named "spec/functional/widgets_controller_spec.rb" with:
|
39
|
+
"""ruby
|
40
|
+
require "spec_helper"
|
41
|
+
|
42
|
+
describe WidgetsController, :type => :controller do
|
43
|
+
it "responds successfully" do
|
44
|
+
get :index
|
45
|
+
expect(response.status).to eq(200)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
"""
|
49
|
+
When I run `rspec spec`
|
50
|
+
Then the example should pass
|
51
|
+
|
52
|
+
Scenario: Specs in canonical directories can override their types
|
53
|
+
Given a file named "spec/routing/duckduck_routing_spec.rb" with:
|
54
|
+
"""ruby
|
55
|
+
require "spec_helper"
|
56
|
+
|
57
|
+
Rails.application.routes.draw do
|
58
|
+
get "/example" => redirect("http://example.com")
|
59
|
+
end
|
60
|
+
|
61
|
+
# Due to limitations in the Rails routing test framework, routes that
|
62
|
+
# perform redirects must actually be tested via request specs
|
63
|
+
describe "/example", :type => :request do
|
64
|
+
it "redirects to example.com" do
|
65
|
+
get "/example"
|
66
|
+
expect(response).to redirect_to("http://example.com")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
"""
|
70
|
+
When I run `rspec spec`
|
71
|
+
Then the example should pass
|