rspec-rails 2.3.1 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +21 -1
- data/README.md +3 -3
- data/Rakefile +2 -2
- data/{Upgrade.markdown → Upgrade.md} +0 -0
- data/features/.nav +12 -7
- data/features/Autotest.md +10 -0
- data/features/Generators.md +8 -0
- data/features/GettingStarted.md +40 -0
- data/features/{README.markdown → README.md} +1 -26
- data/features/Upgrade.md +56 -0
- data/features/controller_specs/README.md +27 -16
- data/features/controller_specs/isolation_from_views.feature +6 -5
- data/features/controller_specs/render_views.feature +30 -1
- data/features/helper_specs/helper_spec.feature +36 -3
- data/features/mocks/mock_model.feature +3 -3
- data/features/model_specs/README.md +19 -0
- data/features/routing_specs/README.md +16 -0
- data/features/{matchers → routing_specs}/be_routable_matcher.feature +18 -18
- data/features/routing_specs/{access_to_named_routes.feature → named_routes.feature} +6 -3
- data/features/routing_specs/route_to_matcher.feature +38 -0
- data/lib/generators/rspec/scaffold/scaffold_generator.rb +6 -6
- data/lib/generators/rspec/scaffold/templates/controller_spec.rb +22 -24
- data/lib/rspec/rails.rb +0 -1
- data/lib/rspec/rails/example.rb +29 -1
- data/lib/rspec/rails/example/controller_example_group.rb +1 -5
- data/lib/rspec/rails/example/helper_example_group.rb +5 -6
- data/lib/rspec/rails/example/mailer_example_group.rb +1 -5
- data/lib/rspec/rails/example/model_example_group.rb +0 -3
- data/lib/rspec/rails/example/request_example_group.rb +1 -5
- data/lib/rspec/rails/example/routing_example_group.rb +1 -5
- data/lib/rspec/rails/example/view_example_group.rb +1 -4
- 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 +1 -1
- data/lib/rspec/rails/module_inclusion.rb +17 -0
- data/lib/rspec/rails/version.rb +1 -1
- data/lib/rspec/rails/view_rendering.rb +23 -7
- data/spec/rspec/rails/configuration_spec.rb +26 -0
- data/spec/rspec/rails/example/helper_example_group_spec.rb +18 -0
- data/spec/rspec/rails/example/request_example_group_spec.rb +2 -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 +64 -33
- data/spec/rspec/rails/view_rendering_spec.rb +105 -0
- metadata +38 -20
- data/spec/rspec/rails/example/view_rendering_spec.rb +0 -110
data/History.md
CHANGED
@@ -1,8 +1,28 @@
|
|
1
1
|
## rspec-rails-2 release history
|
2
2
|
|
3
|
+
### 2.4.0 / 2011-01-02
|
4
|
+
|
5
|
+
[full changelog](http://github.com/rspec/rspec-rails/compare/v2.3.1...2.4.0)
|
6
|
+
|
7
|
+
* Enhancements
|
8
|
+
* include ApplicationHelper in helper object in helper specs
|
9
|
+
* include request spec extensions in files in spec/integration
|
10
|
+
* include controller spec extensions in groups that use :type => :controller
|
11
|
+
* same for :model, :view, :helper, :mailer, :request, :routing
|
12
|
+
|
13
|
+
* Bug fixes
|
14
|
+
* restore global config.render_views so you only need to say it once
|
15
|
+
* support overriding render_views in nested groups
|
16
|
+
* matchers that delegate to Rails' assertions capture
|
17
|
+
ActiveSupport::TestCase::Assertion (so they work properly now with
|
18
|
+
should_not in Ruby 1.8.7 and 1.9.1)
|
19
|
+
|
20
|
+
* Deprecations
|
21
|
+
* include_self_when_dir_matches
|
22
|
+
|
3
23
|
### 2.3.1 / 2010-12-16
|
4
24
|
|
5
|
-
[full changelog](http://github.com/rspec/rspec-rails/compare/v2.3.0...
|
25
|
+
[full changelog](http://github.com/rspec/rspec-rails/compare/v2.3.0...v2.3.1)
|
6
26
|
|
7
27
|
* Bug fixes
|
8
28
|
* respond_to? correctly handles 2 args
|
data/README.md
CHANGED
@@ -60,7 +60,7 @@ out of the box for the default scenario (`ActiveRecord` + `Webrat`).
|
|
60
60
|
|
61
61
|
### Autotest
|
62
62
|
|
63
|
-
The `rspec:install` generator creates an
|
63
|
+
The `rspec:install` generator creates an `.rspec` file, which
|
64
64
|
tells Autotest that you're using RSpec and Rails. You'll also need to add the
|
65
65
|
autotest (not autotest-rails) gem to your Gemfile:
|
66
66
|
|
@@ -142,8 +142,8 @@ You can use RSpec expectations/matchers or Test::Unit assertions.
|
|
142
142
|
## `render_views`
|
143
143
|
By default, controller specs do not render views. This supports specifying
|
144
144
|
controllers without concern for whether the views they render work correctly
|
145
|
-
(NOTE: the template must exist, unlike rspec-rails-1. See Upgrade.
|
146
|
-
|
145
|
+
(NOTE: the template must exist, unlike rspec-rails-1. See Upgrade.md for more
|
146
|
+
information about this). If you prefer to render the views (a la Rails'
|
147
147
|
functional tests), you can use the `render_views` declaration in each example
|
148
148
|
group:
|
149
149
|
|
data/Rakefile
CHANGED
@@ -105,10 +105,10 @@ namespace :clobber do
|
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
desc "Push cukes to relishapp using the relish-client-gem"
|
108
|
+
desc "Push docs/cukes to relishapp using the relish-client-gem"
|
109
109
|
task :relish, :version do |t, args|
|
110
110
|
raise "rake relish[VERSION]" unless args[:version]
|
111
|
-
sh "relish push
|
111
|
+
sh "relish push rspec/rspec-rails:#{args[:version]}"
|
112
112
|
end
|
113
113
|
|
114
114
|
task :default => [:spec, "clobber:app", "generate:app", "generate:stuff", :smoke, :cucumber]
|
File without changes
|
data/features/.nav
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
+
- GettingStarted.md (Getting started)
|
2
|
+
- Upgrade.md
|
3
|
+
- Generators.md (Rails generators)
|
4
|
+
- Autotest.md (Autotest integration)
|
5
|
+
- transactional_examples.feature
|
1
6
|
- model_specs:
|
2
|
-
- transactional_examples.feature
|
3
7
|
- errors_on.feature
|
4
|
-
- view_specs:
|
5
|
-
- view_spec.feature
|
6
|
-
- stub_template.feature
|
7
|
-
- inferred_controller_path.feature
|
8
8
|
- controller_specs:
|
9
9
|
- controller_spec.feature
|
10
10
|
- isolation_from_views.feature
|
@@ -15,12 +15,17 @@
|
|
15
15
|
- mailer_specs:
|
16
16
|
- url_helpers.feature
|
17
17
|
- routing_specs:
|
18
|
-
-
|
18
|
+
- route_to_matcher.feature
|
19
|
+
- be_routable_matcher.feature
|
20
|
+
- named_routes.feature
|
21
|
+
- view_specs:
|
22
|
+
- view_spec.feature
|
23
|
+
- stub_template.feature
|
24
|
+
- inferred_controller_path.feature
|
19
25
|
- matchers:
|
20
26
|
- new_record_matcher.feature
|
21
27
|
- render_template_matcher.feature
|
22
28
|
- redirect_to_matcher.feature
|
23
|
-
- be_routable_matcher.feature
|
24
29
|
- mocks:
|
25
30
|
- mock_model.feature
|
26
31
|
- stub_model.feature
|
@@ -0,0 +1,10 @@
|
|
1
|
+
The rspec:install generator creates a .rspec file, which tells RSpec to tell
|
2
|
+
Autotest that you're using RSpec and Rails. You'll also need to add the ZenTest
|
3
|
+
gem to your Gemfile:
|
4
|
+
|
5
|
+
gem "ZenTest"
|
6
|
+
|
7
|
+
At this point, if all of the gems in your Gemfile are installed in system gems,
|
8
|
+
you can just type autotest. If, however, Bundler is managing any gems for you
|
9
|
+
directly (i.e. you've got :git or :path attributes in the Gemfile), you'll need
|
10
|
+
to run bundle exec autotest.
|
@@ -0,0 +1,8 @@
|
|
1
|
+
If you type script/rails generate, the only RSpec generator you'll actually see
|
2
|
+
is rspec:install. That's because RSpec is registered with Rails as the test
|
3
|
+
framework, so whenever you generate application components like models,
|
4
|
+
controllers, etc, RSpec specs are generated instead of Test::Unit tests.
|
5
|
+
|
6
|
+
Note that the generators are there to help you get started, but they are no
|
7
|
+
substitute for writing your own examples, and they are only guaranteed to work
|
8
|
+
out of the box for the default scenario (ActiveRecord + Webrat).
|
@@ -0,0 +1,40 @@
|
|
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
|
+
### Generate a scaffold
|
19
|
+
|
20
|
+
$ rails generate scaffold Widgets name:string
|
21
|
+
|
22
|
+
This generates files in the `app` and `spec` directories. The files in the
|
23
|
+
`app` directory are generated by Rails, and Rails delegates the generation of
|
24
|
+
the files in the `spec` directory to RSpec.
|
25
|
+
|
26
|
+
### Run migrations
|
27
|
+
|
28
|
+
$ rake db:migrate && rake db:test:prepare
|
29
|
+
|
30
|
+
### Run RSpec
|
31
|
+
|
32
|
+
$ rake spec
|
33
|
+
|
34
|
+
or
|
35
|
+
|
36
|
+
$ rspec spec
|
37
|
+
|
38
|
+
If all went well, you should see output ending with:
|
39
|
+
|
40
|
+
29 examples, 0 failures, 2 pending
|
@@ -1,5 +1,5 @@
|
|
1
1
|
rspec-rails extends Rails' built-in testing framework to support rspec examples
|
2
|
-
for requests, controllers, models, views, and
|
2
|
+
for requests, controllers, models, views, helpers, mailers and routing.
|
3
3
|
|
4
4
|
## Rails-3
|
5
5
|
|
@@ -36,30 +36,6 @@ Now you can run:
|
|
36
36
|
This adds the spec directory and some skeleton files, including a .rspec
|
37
37
|
file.
|
38
38
|
|
39
|
-
## Generators
|
40
|
-
|
41
|
-
If you type script/rails generate, the only RSpec generator you'll actually see
|
42
|
-
is rspec:install. That's because RSpec is registered with Rails as the test
|
43
|
-
framework, so whenever you generate application components like models,
|
44
|
-
controllers, etc, RSpec specs are generated instead of Test::Unit tests.
|
45
|
-
|
46
|
-
Note that the generators are there to help you get started, but they are no
|
47
|
-
substitute for writing your own examples, and they are only guaranteed to work
|
48
|
-
out of the box for the default scenario (ActiveRecord + Webrat).
|
49
|
-
|
50
|
-
## Autotest
|
51
|
-
|
52
|
-
The rspec:install generator creates a .rspec file, which tells RSpec to tell
|
53
|
-
Autotest that you're using RSpec and Rails. You'll also need to add the ZenTest
|
54
|
-
gem to your Gemfile:
|
55
|
-
|
56
|
-
gem "ZenTest"
|
57
|
-
|
58
|
-
At this point, if all of the gems in your Gemfile are installed in system gems,
|
59
|
-
you can just type autotest. If, however, Bundler is managing any gems for you
|
60
|
-
directly (i.e. you've got :git or :path attributes in the Gemfile), you'll need
|
61
|
-
to run bundle exec autotest.
|
62
|
-
|
63
39
|
## Webrat and Capybara
|
64
40
|
|
65
41
|
You can choose between webrat or capybara for simulating a browser, automating
|
@@ -80,4 +56,3 @@ incomplete or confusing, or, better yet, wish to write a missing Cucumber
|
|
80
56
|
feature yourself, please [submit an
|
81
57
|
issue](http://github.com/rspec/rspec-rails/issues) or a [pull
|
82
58
|
request](http://github.com/rspec/rspec-rails).
|
83
|
-
|
data/features/Upgrade.md
ADDED
@@ -0,0 +1,56 @@
|
|
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 spec: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/core'
|
32
|
+
|
33
|
+
RSpec.configure do |config|
|
34
|
+
...
|
35
|
+
end
|
36
|
+
|
37
|
+
## Controller specs
|
38
|
+
|
39
|
+
### `response.should render_template`
|
40
|
+
|
41
|
+
This needs to move from before the action to after. For example:
|
42
|
+
|
43
|
+
# rspec-rails-1
|
44
|
+
controller.should render_template("edit")
|
45
|
+
get :edit, :id => "37"
|
46
|
+
|
47
|
+
# rspec-rails-2
|
48
|
+
get :edit, :id => "37"
|
49
|
+
response.should render_template("edit")
|
50
|
+
|
51
|
+
rspec-1 had to monkey patch Rails to get render_template to work before the
|
52
|
+
action, and this broke a couple of times with Rails releases (requiring urgent
|
53
|
+
fix releases in RSpec). Part of the philosophy of rspec-rails-2 is to rely on
|
54
|
+
public APIs in Rails as much as possible. In this case, `render_template`
|
55
|
+
delegates directly to Rails' `assert_template`, which only works after the
|
56
|
+
action.
|
@@ -1,25 +1,36 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
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). It allows you to simulate a single
|
6
|
+
http request in each example, and then specify expected outcomes, including:
|
4
7
|
|
5
8
|
* templates that are rendered by the action
|
6
|
-
* instance variables that are assigned in the controller to be shared with
|
7
|
-
|
9
|
+
* instance variables that are assigned in the controller to be shared with the
|
10
|
+
view
|
8
11
|
* cookies that get sent back with the response
|
9
12
|
|
10
13
|
To specify outcomes, you can use:
|
11
14
|
|
12
|
-
* standard rspec matchers (response.code.should eq(200))
|
13
|
-
* standard test/unit assertions (assert_equal 200, response.code)
|
14
|
-
* rails assertions (assert_response 200)
|
15
|
+
* standard rspec matchers (`response.code.should eq(200)`)
|
16
|
+
* standard test/unit assertions (`assert_equal 200, response.code`)
|
17
|
+
* rails assertions (`assert_response 200`)
|
15
18
|
* rails-specific matchers:
|
16
|
-
* response.should render_template (wraps assert_template)
|
17
|
-
* response.should redirect_to (wraps assert_redirected_to)
|
18
|
-
* assigns(:widget).should be_a_new(Widget)
|
19
|
+
* `response.should render_template (wraps assert_template)`
|
20
|
+
* `response.should redirect_to (wraps assert_redirected_to)`
|
21
|
+
* `assigns(:widget).should be_a_new(Widget)`
|
19
22
|
|
20
|
-
Conventions:
|
23
|
+
## Conventions:
|
24
|
+
|
25
|
+
### Controller
|
26
|
+
|
27
|
+
* pass the controller being specified to the outermost `describe` method.
|
28
|
+
|
29
|
+
describe AccountController do
|
30
|
+
# ...
|
31
|
+
|
32
|
+
### Views
|
21
33
|
|
22
|
-
*
|
23
|
-
|
24
|
-
|
25
|
-
"render_views" for details
|
34
|
+
* by default, views are not rendered. See
|
35
|
+
[views are stubbed by default](controller-specs/views-are-stubbed-by-default) and
|
36
|
+
[render_views](controller-specs/render-views) for details.
|
@@ -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 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:
|
@@ -1,4 +1,4 @@
|
|
1
|
-
Feature:
|
1
|
+
Feature: render_views
|
2
2
|
|
3
3
|
You can tell a controller example group to render views with the render_views
|
4
4
|
declaration.
|
@@ -101,3 +101,32 @@ Feature: render views
|
|
101
101
|
"""
|
102
102
|
When I run "rspec spec"
|
103
103
|
Then the output should contain "4 examples, 0 failures"
|
104
|
+
|
105
|
+
Scenario: render_views globally
|
106
|
+
Given a file named "spec/support/render_views.rb" with:
|
107
|
+
"""
|
108
|
+
RSpec.configure do |config|
|
109
|
+
config.render_views
|
110
|
+
end
|
111
|
+
"""
|
112
|
+
And a file named "spec/controllers/widgets_controller_spec.rb" with:
|
113
|
+
"""
|
114
|
+
require "spec_helper"
|
115
|
+
|
116
|
+
describe WidgetsController do
|
117
|
+
describe "index" do
|
118
|
+
it "renders the index template" do
|
119
|
+
get :index
|
120
|
+
response.should contain("Listing widgets")
|
121
|
+
end
|
122
|
+
|
123
|
+
it "renders the widgets/index template" do
|
124
|
+
get :index
|
125
|
+
response.should contain("Listing widgets")
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
"""
|
130
|
+
When I run "rspec spec"
|
131
|
+
Then the output should contain "2 examples, 0 failures"
|
132
|
+
|
@@ -1,7 +1,7 @@
|
|
1
1
|
Feature: helper spec
|
2
2
|
|
3
|
-
Helper specs live in spec/helpers
|
4
|
-
|
3
|
+
Helper specs live in `spec/helpers`. In order to access the helper methods
|
4
|
+
you can call them on the `helper` object.
|
5
5
|
|
6
6
|
Scenario: helper method that returns true
|
7
7
|
Given a file named "spec/helpers/application_helper_spec.rb" with:
|
@@ -50,4 +50,37 @@ Feature: helper spec
|
|
50
50
|
end
|
51
51
|
"""
|
52
52
|
When I run "rspec spec/helpers/application_helper_spec.rb"
|
53
|
-
Then the output should contain "1 example, 0 failures"
|
53
|
+
Then the output should contain "1 example, 0 failures"
|
54
|
+
|
55
|
+
Scenario: application helper is included in helper object
|
56
|
+
Given a file named "spec/helpers/widgets_helper_spec.rb" with:
|
57
|
+
"""
|
58
|
+
require "spec_helper"
|
59
|
+
|
60
|
+
describe WidgetsHelper do
|
61
|
+
describe "#page_title" do
|
62
|
+
it "includes the app name" do
|
63
|
+
assign(:title, "This Page")
|
64
|
+
helper.page_title.should eq("The App: This Page")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
"""
|
69
|
+
And a file named "app/helpers/application_helper.rb" with:
|
70
|
+
"""
|
71
|
+
module ApplicationHelper
|
72
|
+
def app_name
|
73
|
+
"The App"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
"""
|
77
|
+
And a file named "app/helpers/widgets_helper.rb" with:
|
78
|
+
"""
|
79
|
+
module WidgetsHelper
|
80
|
+
def page_title
|
81
|
+
"#{app_name}: #{@title}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
"""
|
85
|
+
When I run "rspec spec/helpers/widgets_helper_spec.rb"
|
86
|
+
Then the output should contain "1 example, 0 failures"
|
@@ -1,8 +1,8 @@
|
|
1
1
|
Feature: mock_model
|
2
2
|
|
3
|
-
The mock_model method generates a test double
|
4
|
-
|
5
|
-
|
3
|
+
The mock_model method generates a test double that acts like an Active Model
|
4
|
+
model. This is different from the stub_model method which generates an
|
5
|
+
instance of a real ActiveModel class.
|
6
6
|
|
7
7
|
The benefit of mock_model over stub_model is that its a true double, so the
|
8
8
|
examples are not dependent on the behaviour (or mis-behaviour), or even the
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Model specs live in spec/models, e.g. spec/models/account_spec.rb. A model spec
|
2
|
+
is a thin wrapper for an ActiveSupport::TestCase, and includes all of the
|
3
|
+
behavior and assertions that it provides, in addition to RSpec's own behavior
|
4
|
+
and expectations.
|
5
|
+
|
6
|
+
## Examples
|
7
|
+
|
8
|
+
require "spec_helper"
|
9
|
+
|
10
|
+
describe Post do
|
11
|
+
context "with 2 or more comments" do
|
12
|
+
it "orders them in reverse" do
|
13
|
+
post = Post.create
|
14
|
+
comment1 = post.comment("first")
|
15
|
+
comment2 = post.comment("second")
|
16
|
+
post.reload.comments.should eq([comment2, comment1])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|