rspec-rails 2.14.0.rc1 → 2.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +12 -0
- data/README.md +2 -2
- data/features/Autotest.md +24 -0
- 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/feature_specs/feature_spec.feature +34 -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 +22 -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/env.rb +53 -0
- data/features/support/rails_versions.rb +4 -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/rspec/rails/example/controller_example_group.rb +2 -4
- data/lib/rspec/rails/extensions/active_record/base.rb +8 -2
- data/lib/rspec/rails/mocks.rb +15 -10
- data/lib/rspec/rails/tasks/rspec.rake +7 -7
- data/lib/rspec/rails/version.rb +1 -1
- data/spec/autotest/rails_rspec2_spec.rb +36 -0
- 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 +64 -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/errors_on_spec.rb +38 -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 +379 -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 +31 -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 +192 -18
@@ -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,34 @@
|
|
1
|
+
Feature: feature spec
|
2
|
+
|
3
|
+
Feature specs are high-level tests meant to exercise slices of functionality
|
4
|
+
through an application. They should drive the application only via its
|
5
|
+
external interface, usually web pages.
|
6
|
+
|
7
|
+
Feature specs require the [capybara](http://github.com/jnicklas/capybara)
|
8
|
+
gem, version 2.0.0 or later. Refer to the [capybara API
|
9
|
+
documentation](http://rubydoc.info/github/jnicklas/capybara/master) for more
|
10
|
+
information on the methods and matchers that can be used in feature specs.
|
11
|
+
|
12
|
+
The `feature` and `scenario` DSL correspond to `describe` and `it`,
|
13
|
+
respectively. These methods are simply aliases that allow feature specs to
|
14
|
+
read more as [customer tests](http://c2.com/cgi/wiki?CustomerTest) and
|
15
|
+
[acceptance tests](http://c2.com/cgi/wiki?AcceptanceTest).
|
16
|
+
|
17
|
+
Scenario: specify creating a Widget by driving the application with capybara
|
18
|
+
Given a file named "spec/features/widget_management_spec.rb" with:
|
19
|
+
"""ruby
|
20
|
+
require "spec_helper"
|
21
|
+
|
22
|
+
feature "Widget management" do
|
23
|
+
scenario "User creates a new widget" do
|
24
|
+
visit "/widgets/new"
|
25
|
+
|
26
|
+
fill_in "Name", :with => "My Widget"
|
27
|
+
click_button "Create Widget"
|
28
|
+
|
29
|
+
expect(page).to have_text("Widget was successfully created.")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
"""
|
33
|
+
When I run `rspec spec/features/widget_management_spec.rb`
|
34
|
+
Then the example should pass
|
@@ -0,0 +1,122 @@
|
|
1
|
+
Feature: helper spec
|
2
|
+
|
3
|
+
Helper specs live in `spec/helpers`, or any example group with `:type =>
|
4
|
+
:helper`.
|
5
|
+
|
6
|
+
Helper specs expose a `helper` object, which includes the helper module being
|
7
|
+
specified, the `ApplicationHelper` module (if there is one) and all of the
|
8
|
+
helpers built into Rails. It does not include the other helper modules in
|
9
|
+
your app.
|
10
|
+
|
11
|
+
To access the helper methods you're specifying, simply call them directly
|
12
|
+
on the `helper` object.
|
13
|
+
|
14
|
+
NOTE: helper methods defined in controllers are not included.
|
15
|
+
|
16
|
+
Scenario: helper method that returns a value
|
17
|
+
Given a file named "spec/helpers/application_helper_spec.rb" with:
|
18
|
+
"""ruby
|
19
|
+
require "spec_helper"
|
20
|
+
|
21
|
+
describe ApplicationHelper do
|
22
|
+
describe "#page_title" do
|
23
|
+
it "returns the default title" do
|
24
|
+
expect(helper.page_title).to eq("RSpec is your friend")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
"""
|
29
|
+
And a file named "app/helpers/application_helper.rb" with:
|
30
|
+
"""ruby
|
31
|
+
module ApplicationHelper
|
32
|
+
def page_title
|
33
|
+
"RSpec is your friend"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
"""
|
37
|
+
When I run `rspec spec/helpers/application_helper_spec.rb`
|
38
|
+
Then the examples should all pass
|
39
|
+
|
40
|
+
Scenario: helper method that accesses an instance variable
|
41
|
+
Given a file named "spec/helpers/application_helper_spec.rb" with:
|
42
|
+
"""ruby
|
43
|
+
require "spec_helper"
|
44
|
+
|
45
|
+
describe ApplicationHelper do
|
46
|
+
describe "#page_title" do
|
47
|
+
it "returns the instance variable" do
|
48
|
+
assign(:title, "My Title")
|
49
|
+
expect(helper.page_title).to eql("My Title")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
"""
|
54
|
+
And a file named "app/helpers/application_helper.rb" with:
|
55
|
+
"""ruby
|
56
|
+
module ApplicationHelper
|
57
|
+
def page_title
|
58
|
+
@title || nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
"""
|
62
|
+
When I run `rspec spec/helpers/application_helper_spec.rb`
|
63
|
+
Then the examples should all pass
|
64
|
+
|
65
|
+
Scenario: application helper is included in helper object
|
66
|
+
Given a file named "spec/helpers/widgets_helper_spec.rb" with:
|
67
|
+
"""ruby
|
68
|
+
require "spec_helper"
|
69
|
+
|
70
|
+
describe WidgetsHelper do
|
71
|
+
describe "#widget_title" do
|
72
|
+
it "includes the app name" do
|
73
|
+
assign(:title, "This Widget")
|
74
|
+
expect(helper.widget_title).to eq("The App: This Widget")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
"""
|
79
|
+
And a file named "app/helpers/application_helper.rb" with:
|
80
|
+
"""ruby
|
81
|
+
module ApplicationHelper
|
82
|
+
def app_name
|
83
|
+
"The App"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
"""
|
87
|
+
And a file named "app/helpers/widgets_helper.rb" with:
|
88
|
+
"""ruby
|
89
|
+
module WidgetsHelper
|
90
|
+
def widget_title
|
91
|
+
"#{app_name}: #{@title}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
"""
|
95
|
+
When I run `rspec spec/helpers/widgets_helper_spec.rb`
|
96
|
+
Then the examples should all pass
|
97
|
+
|
98
|
+
Scenario: url helpers are defined
|
99
|
+
Given a file named "spec/helpers/widgets_helper_spec.rb" with:
|
100
|
+
"""ruby
|
101
|
+
require "spec_helper"
|
102
|
+
|
103
|
+
describe WidgetsHelper do
|
104
|
+
describe "#link_to_widget" do
|
105
|
+
it "links to a widget using its name" do
|
106
|
+
widget = Widget.create!(:name => "This Widget")
|
107
|
+
expect(helper.link_to_widget(widget)).to include("This Widget")
|
108
|
+
expect(helper.link_to_widget(widget)).to include(widget_path(widget))
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
"""
|
113
|
+
And a file named "app/helpers/widgets_helper.rb" with:
|
114
|
+
"""ruby
|
115
|
+
module WidgetsHelper
|
116
|
+
def link_to_widget(widget)
|
117
|
+
link_to(widget.name, widget_path(widget))
|
118
|
+
end
|
119
|
+
end
|
120
|
+
"""
|
121
|
+
When I run `rspec spec/helpers/widgets_helper_spec.rb`
|
122
|
+
Then the examples should all pass
|