rspec-rails 2.0.0.beta.22 → 2.6.0
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.
- data/.gitignore +10 -4
- data/.rspec +1 -0
- data/.travis.yml +10 -0
- data/History.md +1 -33
- data/License.txt +23 -0
- data/{README.markdown → README.md} +27 -7
- data/README_DEV.md +43 -0
- data/Rakefile +105 -35
- data/Thorfile +40 -0
- data/Upgrade.md +1 -0
- data/features/.nav +34 -0
- data/features/Autotest.md +24 -0
- data/features/Changelog.md +151 -0
- data/features/Generators.md +8 -0
- data/features/GettingStarted.md +84 -0
- data/features/README.md +58 -0
- data/features/Upgrade.md +117 -0
- data/features/controller_specs/README.md +45 -0
- data/features/controller_specs/anonymous_controller.feature +7 -7
- data/features/controller_specs/controller_spec.feature +18 -0
- data/features/controller_specs/isolation_from_views.feature +55 -8
- data/features/controller_specs/render_views.feature +63 -52
- data/features/helper_specs/helper_spec.feature +40 -6
- data/features/mailer_specs/url_helpers.feature +4 -4
- data/features/matchers/README.md +4 -0
- data/features/matchers/new_record_matcher.feature +6 -48
- data/features/matchers/redirect_to_matcher.feature +5 -6
- data/features/matchers/render_template_matcher.feature +13 -10
- data/features/mocks/mock_model.feature +29 -13
- data/features/mocks/stub_model.feature +4 -4
- data/features/model_specs/README.md +21 -0
- data/features/model_specs/errors_on.feature +2 -2
- data/features/model_specs/transactional_examples.feature +14 -9
- data/features/request_specs/request_spec.feature +49 -0
- data/features/routing_specs/README.md +17 -0
- data/features/{matchers → routing_specs}/be_routable_matcher.feature +25 -25
- data/features/routing_specs/named_routes.feature +18 -0
- data/features/routing_specs/route_to_matcher.feature +58 -0
- data/features/step_definitions/additional_cli_steps.rb +4 -0
- data/features/support/env.rb +14 -6
- data/features/view_specs/inferred_controller_path.feature +6 -6
- data/features/view_specs/stub_template.feature +51 -0
- data/features/view_specs/view_spec.feature +23 -42
- data/gemfiles/.bundle/config +2 -0
- data/gemfiles/base.rb +58 -0
- data/gemfiles/rails-3-0-stable +6 -0
- data/gemfiles/rails-3.0.0 +5 -0
- data/gemfiles/rails-3.0.1 +5 -0
- data/gemfiles/rails-3.0.2 +5 -0
- data/gemfiles/rails-3.0.3 +5 -0
- data/gemfiles/rails-3.0.4 +5 -0
- data/gemfiles/rails-3.0.5 +5 -0
- data/gemfiles/rails-3.0.6 +5 -0
- data/gemfiles/rails-3.0.7 +5 -0
- data/gemfiles/rails-3.1.0.beta1 +5 -0
- data/gemfiles/rails-master +7 -0
- data/lib/autotest/rails_rspec2.rb +1 -1
- data/lib/generators/rspec/controller/controller_generator.rb +4 -4
- data/lib/generators/rspec/helper/helper_generator.rb +3 -3
- data/lib/generators/rspec/install/install_generator.rb +0 -8
- data/lib/generators/rspec/integration/integration_generator.rb +15 -1
- data/lib/generators/rspec/integration/templates/request_spec.rb +7 -1
- data/lib/generators/rspec/mailer/mailer_generator.rb +2 -2
- data/lib/generators/rspec/model/model_generator.rb +1 -1
- data/lib/generators/rspec/observer/observer_generator.rb +1 -1
- data/lib/generators/rspec/scaffold/scaffold_generator.rb +40 -11
- data/lib/generators/rspec/scaffold/templates/controller_spec.rb +90 -58
- data/lib/generators/rspec/scaffold/templates/edit_spec.rb +15 -7
- data/lib/generators/rspec/scaffold/templates/index_spec.rb +7 -2
- data/lib/generators/rspec/scaffold/templates/new_spec.rb +14 -7
- data/lib/generators/rspec/scaffold/templates/routing_spec.rb +14 -14
- data/lib/generators/rspec/scaffold/templates/show_spec.rb +7 -2
- data/lib/generators/rspec/view/view_generator.rb +1 -1
- data/lib/rspec/rails/example/controller_example_group.rb +25 -34
- data/lib/rspec/rails/example/helper_example_group.rb +5 -11
- data/lib/rspec/rails/example/mailer_example_group.rb +0 -14
- data/lib/rspec/rails/example/model_example_group.rb +0 -4
- data/lib/rspec/rails/example/rails_example_group.rb +0 -2
- data/lib/rspec/rails/example/request_example_group.rb +5 -30
- data/lib/rspec/rails/example/routing_example_group.rb +3 -5
- data/lib/rspec/rails/example/view_example_group.rb +20 -11
- data/lib/rspec/rails/example.rb +31 -1
- data/lib/rspec/rails/extensions/active_record/base.rb +28 -32
- data/lib/rspec/rails/fixture_support.rb +1 -1
- data/lib/rspec/rails/matchers/be_new_record.rb +5 -0
- data/lib/rspec/rails/matchers/redirect_to.rb +6 -2
- data/lib/rspec/rails/matchers/render_template.rb +5 -1
- data/lib/rspec/rails/matchers/routing_matchers.rb +24 -3
- data/lib/rspec/rails/matchers.rb +1 -1
- data/lib/rspec/rails/mocks.rb +118 -79
- data/lib/rspec/rails/module_inclusion.rb +17 -0
- data/lib/rspec/rails/tasks/rspec.rake +1 -1
- data/lib/rspec/rails/vendor/capybara.rb +29 -0
- data/lib/rspec/rails/vendor/webrat.rb +33 -0
- data/lib/rspec/rails/version.rb +1 -1
- data/lib/rspec/rails/view_assigns.rb +23 -4
- data/lib/rspec/rails/view_rendering.rb +50 -13
- data/lib/rspec/rails.rb +7 -10
- data/lib/rspec-rails.rb +4 -2
- data/rspec-rails.gemspec +11 -39
- data/spec/autotest/rails_rspec2_spec.rb +25 -14
- 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 +47 -5
- data/spec/rspec/rails/example/helper_example_group_spec.rb +19 -1
- data/spec/rspec/rails/example/request_example_group_spec.rb +2 -0
- data/spec/rspec/rails/extensions/active_model/errors_on_spec.rb +23 -0
- data/spec/rspec/rails/extensions/active_record/records_spec.rb +9 -0
- data/spec/rspec/rails/matchers/be_new_record_spec.rb +17 -0
- data/spec/rspec/rails/matchers/be_routable_spec.rb +41 -0
- data/spec/rspec/rails/matchers/redirect_to_spec.rb +67 -8
- data/spec/rspec/rails/matchers/render_template_spec.rb +67 -10
- data/spec/rspec/rails/matchers/route_to_spec.rb +77 -31
- data/spec/rspec/rails/mocks/ar_classes.rb +17 -25
- data/spec/rspec/rails/mocks/mock_model_spec.rb +50 -13
- data/spec/rspec/rails/mocks/stub_model_spec.rb +121 -67
- data/spec/rspec/rails/view_rendering_spec.rb +105 -0
- data/spec/spec_helper.rb +0 -22
- data/spec/support/matchers.rb +9 -0
- data/templates/generate_stuff.rb +10 -2
- data/templates/run_specs.rb +2 -2
- metadata +141 -79
- data/Gemfile +0 -24
- data/Gotchas.markdown +0 -14
- data/Upgrade.markdown +0 -31
- data/autotest/discover.rb +0 -1
- data/features/README.markdown +0 -12
- data/features/controller_specs/readers.feature +0 -18
- data/features/routing_specs/access_to_named_routes.feature +0 -15
- data/lib/generators/rspec/install/templates/autotest/discover.rb +0 -2
- data/lib/rspec/rails/browser_simulators.rb +0 -30
- data/spec/rspec/rails/example/view_rendering_spec.rb +0 -110
- data/specs.watchr +0 -59
- data/templates/Gemfile +0 -18
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Install Rails-3
|
|
2
|
+
|
|
3
|
+
$ gem install rails -v "~> 3.0.0"
|
|
4
|
+
|
|
5
|
+
### Generate an app
|
|
6
|
+
|
|
7
|
+
$ rails new example
|
|
8
|
+
$ cd example
|
|
9
|
+
|
|
10
|
+
### Add rspec-rails to the Gemfile
|
|
11
|
+
|
|
12
|
+
$ echo 'gem "rspec-rails", :group => [:development, :test]' >> Gemfile
|
|
13
|
+
|
|
14
|
+
### Install the bundle
|
|
15
|
+
|
|
16
|
+
$ bundle install
|
|
17
|
+
|
|
18
|
+
### Bootstrap RSpec
|
|
19
|
+
|
|
20
|
+
$ rails generate rspec:install
|
|
21
|
+
|
|
22
|
+
### Generate a scaffold
|
|
23
|
+
|
|
24
|
+
$ rails generate scaffold Widgets name:string
|
|
25
|
+
|
|
26
|
+
This generates files in the `app` and `spec` directories. The files in the
|
|
27
|
+
`app` directory are generated by Rails, and Rails delegates the generation of
|
|
28
|
+
the files in the `spec` directory to RSpec.
|
|
29
|
+
|
|
30
|
+
### Run migrations
|
|
31
|
+
|
|
32
|
+
$ rake db:migrate && rake db:test:prepare
|
|
33
|
+
|
|
34
|
+
### Run RSpec
|
|
35
|
+
|
|
36
|
+
$ rake spec
|
|
37
|
+
|
|
38
|
+
or
|
|
39
|
+
|
|
40
|
+
$ rspec spec --format documentation
|
|
41
|
+
|
|
42
|
+
If all went well, you should see output ending with:
|
|
43
|
+
|
|
44
|
+
29 examples, 0 failures, 2 pending
|
|
45
|
+
|
|
46
|
+
This output also includes the following controller spec:
|
|
47
|
+
|
|
48
|
+
WidgetsController
|
|
49
|
+
GET index
|
|
50
|
+
assigns all widgets as @widgets
|
|
51
|
+
GET show
|
|
52
|
+
assigns the requested widget as @widget
|
|
53
|
+
GET new
|
|
54
|
+
assigns a new widget as @widget
|
|
55
|
+
GET edit
|
|
56
|
+
assigns the requested widget as @widget
|
|
57
|
+
POST create
|
|
58
|
+
with valid params
|
|
59
|
+
creates a new Widget
|
|
60
|
+
assigns a newly created widget as @widget
|
|
61
|
+
redirects to the created widget
|
|
62
|
+
with invalid params
|
|
63
|
+
assigns a newly created but unsaved widget as @widget
|
|
64
|
+
re-renders the 'new' template
|
|
65
|
+
PUT update
|
|
66
|
+
with valid params
|
|
67
|
+
updates the requested widget
|
|
68
|
+
assigns the requested widget as @widget
|
|
69
|
+
redirects to the widget
|
|
70
|
+
with invalid params
|
|
71
|
+
assigns the widget as @widget
|
|
72
|
+
re-renders the 'edit' template
|
|
73
|
+
DELETE destroy
|
|
74
|
+
destroys the requested widget
|
|
75
|
+
redirects to the widgets list
|
|
76
|
+
|
|
77
|
+
Output like this can help to quickly gain a high level understanding of how an
|
|
78
|
+
object behaves. It also exposes which cases have been specified and which have
|
|
79
|
+
not. Note the balance between the examples for the `create` and `update`
|
|
80
|
+
actions. If the `redirects to the widget` example was missing from one or the
|
|
81
|
+
other, it would be easy to spot.
|
|
82
|
+
|
|
83
|
+
Take a look at the generated `spec/controllers/widgets_controller_spec.rb` to
|
|
84
|
+
get a sense of how to organize your specs to generate output like this.
|
data/features/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
rspec-rails extends Rails' built-in testing framework to support rspec examples
|
|
2
|
+
for requests, controllers, models, views, helpers, mailers and routing.
|
|
3
|
+
|
|
4
|
+
## Rails-3
|
|
5
|
+
|
|
6
|
+
rspec-rails-2 supports rails-3.0.0 and later. For earlier versions of Rails,
|
|
7
|
+
you need [rspec-rails-1.3](http://rspec.info).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
gem install rspec-rails
|
|
12
|
+
|
|
13
|
+
This installs the following gems:
|
|
14
|
+
|
|
15
|
+
rspec
|
|
16
|
+
rspec-core
|
|
17
|
+
rspec-expectations
|
|
18
|
+
rspec-mocks
|
|
19
|
+
rspec-rails
|
|
20
|
+
|
|
21
|
+
## Configure
|
|
22
|
+
|
|
23
|
+
Add rspec-rails to the :test and :development groups in the Gemfile:
|
|
24
|
+
|
|
25
|
+
group :test, :development do
|
|
26
|
+
gem "rspec-rails", "~> 2.4"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
It needs to be in the :development group to expose generators and rake tasks
|
|
30
|
+
without having to type RAILS_ENV=test.
|
|
31
|
+
|
|
32
|
+
Now you can run:
|
|
33
|
+
|
|
34
|
+
script/rails generate rspec:install
|
|
35
|
+
|
|
36
|
+
This adds the spec directory and some skeleton files, including a .rspec
|
|
37
|
+
file.
|
|
38
|
+
|
|
39
|
+
## Webrat and Capybara
|
|
40
|
+
|
|
41
|
+
You can choose between webrat or capybara for simulating a browser, automating
|
|
42
|
+
a browser, or setting expectations using the matchers they supply. Just add
|
|
43
|
+
your preference to the Gemfile:
|
|
44
|
+
|
|
45
|
+
gem "webrat"
|
|
46
|
+
gem "capybara"
|
|
47
|
+
|
|
48
|
+
Note that Capybara matchers are not available in view or helper specs.
|
|
49
|
+
|
|
50
|
+
## Issues
|
|
51
|
+
|
|
52
|
+
The documentation for rspec-rails is a work in progress. We'll be adding
|
|
53
|
+
Cucumber features over time, and clarifying existing ones. If you have
|
|
54
|
+
specific features you'd like to see added, find the existing documentation
|
|
55
|
+
incomplete or confusing, or, better yet, wish to write a missing Cucumber
|
|
56
|
+
feature yourself, please [submit an
|
|
57
|
+
issue](http://github.com/rspec/rspec-rails/issues) or a [pull
|
|
58
|
+
request](http://github.com/rspec/rspec-rails).
|
data/features/Upgrade.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# Upgrading from rspec-rails-1.x to rspec-rails-2.
|
|
2
|
+
|
|
3
|
+
This is a work in progress. Please submit errata, missing steps, or patches to
|
|
4
|
+
the [rspec-rails issue tracker](https://github.com/rspec/rspec-rails/issues).
|
|
5
|
+
|
|
6
|
+
## Rake tasks
|
|
7
|
+
|
|
8
|
+
Delete lib/tasks/rspec.rake, if present. Rake tasks now live in the rspec-rails
|
|
9
|
+
gem.
|
|
10
|
+
|
|
11
|
+
## `spec_helper.rb`
|
|
12
|
+
|
|
13
|
+
There were a few changes to the generated `spec/spec_helper.rb` file. We
|
|
14
|
+
recommend the following:
|
|
15
|
+
|
|
16
|
+
1. set aside a copy of your existing `spec/spec_helper.rb` file.
|
|
17
|
+
2. run `rails generate rspec:install`
|
|
18
|
+
3. copy any customizations from your old spec_helper to the new one
|
|
19
|
+
|
|
20
|
+
If you prefer to make the changes manually in the existing spec_helper, here
|
|
21
|
+
is what you need to change:
|
|
22
|
+
|
|
23
|
+
# rspec-1
|
|
24
|
+
require 'spec/autorun'
|
|
25
|
+
|
|
26
|
+
Spec::Runner.configure do |config|
|
|
27
|
+
...
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# rspec-2
|
|
31
|
+
require 'rspec/rails'
|
|
32
|
+
|
|
33
|
+
RSpec.configure do |config|
|
|
34
|
+
...
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
## Controller specs
|
|
38
|
+
|
|
39
|
+
### islation from view templates
|
|
40
|
+
|
|
41
|
+
By default, controller specs do _not_ render view templates. This keeps
|
|
42
|
+
controller specs isolated from the content of views and their requirements.
|
|
43
|
+
|
|
44
|
+
NOTE that the template must exist, but it will not be rendered. This is
|
|
45
|
+
different from rspec-rails-1.x, in which the template didn't need to exist, but
|
|
46
|
+
ActionController makes a number of new decisions in Rails 3 based on the
|
|
47
|
+
existence of the template. To keep the RSpec code free of monkey patches, and
|
|
48
|
+
to keep the rspec user experience simpler, we decided that this would be a fair
|
|
49
|
+
trade-off.
|
|
50
|
+
|
|
51
|
+
### `response.should render_template`
|
|
52
|
+
|
|
53
|
+
This needs to move from before the action to after. For example:
|
|
54
|
+
|
|
55
|
+
# rspec-rails-1
|
|
56
|
+
controller.should render_template("edit")
|
|
57
|
+
get :edit, :id => "37"
|
|
58
|
+
|
|
59
|
+
# rspec-rails-2
|
|
60
|
+
get :edit, :id => "37"
|
|
61
|
+
response.should render_template("edit")
|
|
62
|
+
|
|
63
|
+
rspec-1 had to monkey patch Rails to get render_template to work before the
|
|
64
|
+
action, and this broke a couple of times with Rails releases (requiring urgent
|
|
65
|
+
fix releases in RSpec). Part of the philosophy of rspec-rails-2 is to rely on
|
|
66
|
+
public APIs in Rails as much as possible. In this case, `render_template`
|
|
67
|
+
delegates directly to Rails' `assert_template`, which only works after the
|
|
68
|
+
action.
|
|
69
|
+
|
|
70
|
+
## View specs
|
|
71
|
+
|
|
72
|
+
### `view.should render_template`
|
|
73
|
+
|
|
74
|
+
Rails changed the way it renders partials, so to set an expectation that a
|
|
75
|
+
partial gets rendered, you need
|
|
76
|
+
|
|
77
|
+
render
|
|
78
|
+
view.should render_template(:partial => "widget/_row")
|
|
79
|
+
|
|
80
|
+
### stub_template
|
|
81
|
+
|
|
82
|
+
Introduced in rspec-rails-2.2, simulates the presence of view templates on the
|
|
83
|
+
file system. This supports isolation from partials rendered by the vew template
|
|
84
|
+
that is the subject of a view example:
|
|
85
|
+
|
|
86
|
+
stub_template "widgets/_widget.html.erb" => "This Content"
|
|
87
|
+
|
|
88
|
+
### No more `have_tag` or `have_text`
|
|
89
|
+
|
|
90
|
+
Before Webrat came along, rspec-rails had its own `have_tag` and `have_text`
|
|
91
|
+
matchers that wrapped Rails' `assert_select`. Webrat included replacements for
|
|
92
|
+
these methods, as well as new matchers (`have_selector` and `have_xpath`), all
|
|
93
|
+
of which rely on Nokogiri to do its work, and are far less brittle than RSpec's
|
|
94
|
+
`have_tag`.
|
|
95
|
+
|
|
96
|
+
Capybara has similar matchers, which will soon be available view specs (they
|
|
97
|
+
are already available in controller specs with `render_views`).
|
|
98
|
+
|
|
99
|
+
Given the brittleness of RSpec's `have_tag` and `have_text` matchers and the
|
|
100
|
+
presence of new Webrat and Capybara matchers that do a better job, `have_tag`
|
|
101
|
+
and `have_text` were not included in rspec-rails-2.
|
|
102
|
+
|
|
103
|
+
## Mocks, stubs, doubles
|
|
104
|
+
|
|
105
|
+
### as_new_record
|
|
106
|
+
|
|
107
|
+
Earlier versions of the view generators generated stub_model with `:new_record?
|
|
108
|
+
=> true`. That is no longer recognized in rspec-rails-2, so you need to change
|
|
109
|
+
this:
|
|
110
|
+
|
|
111
|
+
stub_model(Widget, :new_record? => true)
|
|
112
|
+
|
|
113
|
+
to this:
|
|
114
|
+
|
|
115
|
+
stub_model(Widget).as_new_record
|
|
116
|
+
|
|
117
|
+
Generators in 2.0.0 final release will do the latter.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Controller specs live in `spec/controllers` or any example group with
|
|
2
|
+
`:type => :controller`.
|
|
3
|
+
|
|
4
|
+
A controller spec is an RSpec wrapper for a Rails functional test
|
|
5
|
+
([ActionController::TestCase::Behavior](https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/test_case.rb)).
|
|
6
|
+
It allows you to simulate a single http request in each example, and then
|
|
7
|
+
specify expected outcomes such as:
|
|
8
|
+
|
|
9
|
+
* rendered templates
|
|
10
|
+
* redirects
|
|
11
|
+
* instance variables assigned in the controller to be shared with the view
|
|
12
|
+
* cookies sent back with the response
|
|
13
|
+
|
|
14
|
+
To specify outcomes, you can use:
|
|
15
|
+
|
|
16
|
+
* standard rspec matchers (`response.code.should eq(200)`)
|
|
17
|
+
* standard test/unit assertions (`assert_equal 200, response.code`)
|
|
18
|
+
* rails assertions (`assert_response 200`)
|
|
19
|
+
* rails-specific matchers:
|
|
20
|
+
* `response.should render_template (wraps assert_template)`
|
|
21
|
+
* `response.should redirect_to (wraps assert_redirected_to)`
|
|
22
|
+
* `assigns(:widget).should be_a_new(Widget)`
|
|
23
|
+
|
|
24
|
+
## Examples
|
|
25
|
+
|
|
26
|
+
describe TeamsController do
|
|
27
|
+
describe "GET index" do
|
|
28
|
+
it "assigns @teams" do
|
|
29
|
+
team = Team.create
|
|
30
|
+
get :index
|
|
31
|
+
assigns(:teams).should eq([team])
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "renders the index template" do
|
|
35
|
+
get :index
|
|
36
|
+
response.should render_template("index")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
## Views
|
|
42
|
+
|
|
43
|
+
* by default, views are not rendered. See
|
|
44
|
+
[views are stubbed by default](controller-specs/views-are-stubbed-by-default) and
|
|
45
|
+
[render_views](controller-specs/render-views) for details.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Feature: anonymous controller
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
Use the `controller` method to define an anonymous controller derived from
|
|
4
|
+
ApplicationController, or any other base controller. This is useful for
|
|
5
|
+
specifying behavior like global error handling.
|
|
6
6
|
|
|
7
7
|
Scenario: specify error handling in ApplicationController
|
|
8
8
|
Given a file named "spec/controllers/application_controller_spec.rb" with:
|
|
@@ -36,8 +36,8 @@ Feature: anonymous controller
|
|
|
36
36
|
end
|
|
37
37
|
end
|
|
38
38
|
"""
|
|
39
|
-
When I run
|
|
40
|
-
Then the
|
|
39
|
+
When I run `rspec spec`
|
|
40
|
+
Then the examples should all pass
|
|
41
41
|
|
|
42
42
|
Scenario: specify error handling in subclass of ApplicationController
|
|
43
43
|
Given a file named "spec/controllers/application_controller_subclass_spec.rb" with:
|
|
@@ -74,5 +74,5 @@ Feature: anonymous controller
|
|
|
74
74
|
end
|
|
75
75
|
end
|
|
76
76
|
"""
|
|
77
|
-
When I run
|
|
78
|
-
Then the
|
|
77
|
+
When I run `rspec spec`
|
|
78
|
+
Then the examples should all pass
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Feature: controller spec
|
|
2
|
+
|
|
3
|
+
Scenario: simple passing example
|
|
4
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
|
5
|
+
"""
|
|
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
|
+
response.code.should eq("200")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
"""
|
|
17
|
+
When I run `rspec spec`
|
|
18
|
+
Then the example should pass
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
Feature:
|
|
1
|
+
Feature: views are stubbed by default
|
|
2
2
|
|
|
3
|
-
By default, controller specs
|
|
4
|
-
|
|
5
|
-
|
|
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.
|
|
6
7
|
|
|
7
|
-
NOTE: unlike rspec-rails-1.x, the template must exist.
|
|
8
|
+
NOTE: unlike rspec-rails-1.x, the real template must exist.
|
|
8
9
|
|
|
9
10
|
Scenario: expect template that is rendered by controller action (passes)
|
|
10
11
|
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
|
@@ -26,8 +27,8 @@ Feature: do not render views
|
|
|
26
27
|
end
|
|
27
28
|
end
|
|
28
29
|
"""
|
|
29
|
-
When I run
|
|
30
|
-
Then the
|
|
30
|
+
When I run `rspec spec`
|
|
31
|
+
Then the examples should all pass
|
|
31
32
|
|
|
32
33
|
Scenario: expect template that is not rendered by controller action (fails)
|
|
33
34
|
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
|
@@ -43,6 +44,52 @@ Feature: do not render views
|
|
|
43
44
|
end
|
|
44
45
|
end
|
|
45
46
|
"""
|
|
46
|
-
When I run
|
|
47
|
+
When I run `rspec spec`
|
|
47
48
|
Then the output should contain "1 example, 1 failure"
|
|
48
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
|
+
"""
|
|
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
|
+
response.should render_template("custom_action")
|
|
62
|
+
response.body.should == ""
|
|
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 when view path is changed at runtime (fails)
|
|
71
|
+
Given a file named "spec/controllers/things_controller_spec.rb" with:
|
|
72
|
+
"""
|
|
73
|
+
require "spec_helper"
|
|
74
|
+
|
|
75
|
+
describe ThingsController do
|
|
76
|
+
describe "custom_action" do
|
|
77
|
+
it "renders the custom_action template" do
|
|
78
|
+
render_views
|
|
79
|
+
controller.prepend_view_path 'app/views'
|
|
80
|
+
get :custom_action
|
|
81
|
+
response.should render_template("custom_action")
|
|
82
|
+
response.body.should == ""
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "renders an empty custom_action template" do
|
|
86
|
+
controller.prepend_view_path 'app/views'
|
|
87
|
+
get :custom_action
|
|
88
|
+
response.should render_template("custom_action")
|
|
89
|
+
response.body.should == ""
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
"""
|
|
94
|
+
When I run `rspec spec`
|
|
95
|
+
Then the output should contain "2 examples, 1 failure"
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
Feature:
|
|
1
|
+
Feature: render_views
|
|
2
2
|
|
|
3
|
-
You can tell a controller example group to render views with the
|
|
4
|
-
declaration.
|
|
3
|
+
You can tell a controller example group to render views with the
|
|
4
|
+
`render_views` declaration in any individual group, or globally.
|
|
5
5
|
|
|
6
|
-
Scenario:
|
|
6
|
+
Scenario: render_views directly in a single group
|
|
7
7
|
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
|
8
8
|
"""
|
|
9
9
|
require "spec_helper"
|
|
@@ -11,48 +11,18 @@ Feature: render views
|
|
|
11
11
|
describe WidgetsController do
|
|
12
12
|
render_views
|
|
13
13
|
|
|
14
|
-
describe "index" do
|
|
15
|
-
it "
|
|
16
|
-
get :index
|
|
17
|
-
response.should contain("Listing widgets")
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it "renders the widgets/index template" do
|
|
21
|
-
get :index
|
|
22
|
-
response.should contain("Listing widgets")
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
"""
|
|
27
|
-
When I run "rspec spec"
|
|
28
|
-
Then the output should contain "2 examples, 0 failures"
|
|
29
|
-
|
|
30
|
-
Scenario: expect template that does not exist and is rendered by controller (fails)
|
|
31
|
-
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
|
32
|
-
"""
|
|
33
|
-
require "spec_helper"
|
|
34
|
-
|
|
35
|
-
describe WidgetsController do
|
|
36
|
-
render_views
|
|
37
|
-
|
|
38
|
-
before do
|
|
39
|
-
def controller.index
|
|
40
|
-
render :template => "other"
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
describe "index" do
|
|
45
|
-
it "renders the other template" do
|
|
14
|
+
describe "GET index" do
|
|
15
|
+
it "says 'Listing widgets'" do
|
|
46
16
|
get :index
|
|
17
|
+
response.body.should =~ /Listing widgets/m
|
|
47
18
|
end
|
|
48
19
|
end
|
|
49
20
|
end
|
|
50
21
|
"""
|
|
51
|
-
When I run
|
|
52
|
-
Then the
|
|
53
|
-
And the output should contain "Missing template"
|
|
22
|
+
When I run `rspec spec`
|
|
23
|
+
Then the examples should all pass
|
|
54
24
|
|
|
55
|
-
Scenario: render_views on and off in
|
|
25
|
+
Scenario: render_views on and off in nested groups
|
|
56
26
|
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
|
57
27
|
"""
|
|
58
28
|
require "spec_helper"
|
|
@@ -61,19 +31,30 @@ Feature: render views
|
|
|
61
31
|
context "with render_views" do
|
|
62
32
|
render_views
|
|
63
33
|
|
|
64
|
-
describe "index" do
|
|
34
|
+
describe "GET index" do
|
|
65
35
|
it "renders the actual template" do
|
|
66
36
|
get :index
|
|
67
37
|
response.body.should =~ /Listing widgets/m
|
|
68
38
|
end
|
|
69
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
|
+
response.body.should eq("")
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
70
51
|
end
|
|
71
52
|
|
|
72
53
|
context "without render_views" do
|
|
73
|
-
describe "index" do
|
|
54
|
+
describe "GET index" do
|
|
74
55
|
it "renders the RSpec generated template" do
|
|
75
56
|
get :index
|
|
76
|
-
response.body.should
|
|
57
|
+
response.body.should eq("")
|
|
77
58
|
end
|
|
78
59
|
end
|
|
79
60
|
end
|
|
@@ -81,23 +62,53 @@ Feature: render views
|
|
|
81
62
|
context "with render_views again" do
|
|
82
63
|
render_views
|
|
83
64
|
|
|
84
|
-
describe "index" do
|
|
65
|
+
describe "GET index" do
|
|
85
66
|
it "renders the actual template" do
|
|
86
67
|
get :index
|
|
87
68
|
response.body.should =~ /Listing widgets/m
|
|
88
69
|
end
|
|
89
70
|
end
|
|
90
71
|
end
|
|
72
|
+
end
|
|
73
|
+
"""
|
|
74
|
+
When I run `rspec spec --format documentation`
|
|
75
|
+
Then the output should contain:
|
|
76
|
+
"""
|
|
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
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
92
|
+
Scenario: render_views globally
|
|
93
|
+
Given a file named "spec/support/render_views.rb" with:
|
|
94
|
+
"""
|
|
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
|
+
"""
|
|
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
|
+
response.body.should =~ /Listing widgets/m
|
|
98
108
|
end
|
|
99
109
|
end
|
|
100
110
|
end
|
|
101
111
|
"""
|
|
102
|
-
When I run
|
|
103
|
-
Then the
|
|
112
|
+
When I run `rspec spec`
|
|
113
|
+
Then the examples should all pass
|
|
114
|
+
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
Feature: helper spec
|
|
2
2
|
|
|
3
|
-
Helper specs live in spec/helpers
|
|
4
|
-
the helper methods you can call them on the
|
|
3
|
+
Helper specs live in `spec/helpers`, or any example group with `:type =>
|
|
4
|
+
:helper`. In order to access the helper methods you can call them on the
|
|
5
|
+
`helper` object.
|
|
5
6
|
|
|
6
7
|
Scenario: helper method that returns true
|
|
7
8
|
Given a file named "spec/helpers/application_helper_spec.rb" with:
|
|
@@ -24,8 +25,8 @@ Feature: helper spec
|
|
|
24
25
|
end
|
|
25
26
|
end
|
|
26
27
|
"""
|
|
27
|
-
When I run
|
|
28
|
-
Then the
|
|
28
|
+
When I run `rspec spec/helpers/application_helper_spec.rb`
|
|
29
|
+
Then the examples should all pass
|
|
29
30
|
|
|
30
31
|
Scenario: helper method that accesses an instance variable
|
|
31
32
|
Given a file named "spec/helpers/application_helper_spec.rb" with:
|
|
@@ -49,5 +50,38 @@ Feature: helper spec
|
|
|
49
50
|
end
|
|
50
51
|
end
|
|
51
52
|
"""
|
|
52
|
-
When I run
|
|
53
|
-
Then the
|
|
53
|
+
When I run `rspec spec/helpers/application_helper_spec.rb`
|
|
54
|
+
Then the examples should all pass
|
|
55
|
+
|
|
56
|
+
Scenario: application helper is included in helper object
|
|
57
|
+
Given a file named "spec/helpers/widgets_helper_spec.rb" with:
|
|
58
|
+
"""
|
|
59
|
+
require "spec_helper"
|
|
60
|
+
|
|
61
|
+
describe WidgetsHelper do
|
|
62
|
+
describe "#page_title" do
|
|
63
|
+
it "includes the app name" do
|
|
64
|
+
assign(:title, "This Page")
|
|
65
|
+
helper.page_title.should eq("The App: This Page")
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
"""
|
|
70
|
+
And a file named "app/helpers/application_helper.rb" with:
|
|
71
|
+
"""
|
|
72
|
+
module ApplicationHelper
|
|
73
|
+
def app_name
|
|
74
|
+
"The App"
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
"""
|
|
78
|
+
And a file named "app/helpers/widgets_helper.rb" with:
|
|
79
|
+
"""
|
|
80
|
+
module WidgetsHelper
|
|
81
|
+
def page_title
|
|
82
|
+
"#{app_name}: #{@title}"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
"""
|
|
86
|
+
When I run `rspec spec/helpers/widgets_helper_spec.rb`
|
|
87
|
+
Then the examples should all pass
|
|
@@ -15,8 +15,8 @@ Feature: URL helpers in mailer examples
|
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
"""
|
|
18
|
-
When I run
|
|
19
|
-
Then the
|
|
18
|
+
When I run `rspec spec`
|
|
19
|
+
Then the examples should all pass
|
|
20
20
|
|
|
21
21
|
Scenario: using URL helpers without default options
|
|
22
22
|
Given a file named "config/initializers/mailer_defaults.rb" with:
|
|
@@ -34,5 +34,5 @@ Feature: URL helpers in mailer examples
|
|
|
34
34
|
end
|
|
35
35
|
end
|
|
36
36
|
"""
|
|
37
|
-
When I run
|
|
38
|
-
Then the
|
|
37
|
+
When I run `rspec spec`
|
|
38
|
+
Then the examples should all pass
|