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,35 @@
|
|
1
|
+
@capybara
|
2
|
+
Feature: feature spec
|
3
|
+
|
4
|
+
Feature specs are high-level tests meant to exercise slices of functionality
|
5
|
+
through an application. They should drive the application only via its
|
6
|
+
external interface, usually web pages.
|
7
|
+
|
8
|
+
Feature specs require the [capybara](http://github.com/jnicklas/capybara)
|
9
|
+
gem, version 2.0.0 or later. Refer to the [capybara API
|
10
|
+
documentation](http://rubydoc.info/github/jnicklas/capybara/master) for more
|
11
|
+
information on the methods and matchers that can be used in feature specs.
|
12
|
+
|
13
|
+
The `feature` and `scenario` DSL correspond to `describe` and `it`,
|
14
|
+
respectively. These methods are simply aliases that allow feature specs to
|
15
|
+
read more as [customer tests](http://c2.com/cgi/wiki?CustomerTest) and
|
16
|
+
[acceptance tests](http://c2.com/cgi/wiki?AcceptanceTest).
|
17
|
+
|
18
|
+
Scenario: specify creating a Widget by driving the application with capybara
|
19
|
+
Given a file named "spec/features/widget_management_spec.rb" with:
|
20
|
+
"""ruby
|
21
|
+
require "spec_helper"
|
22
|
+
|
23
|
+
feature "Widget management" do
|
24
|
+
scenario "User creates a new widget" do
|
25
|
+
visit "/widgets/new"
|
26
|
+
|
27
|
+
fill_in "Name", :with => "My Widget"
|
28
|
+
click_button "Create Widget"
|
29
|
+
|
30
|
+
expect(page).to have_text("Widget was successfully created.")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
"""
|
34
|
+
When I run `rspec spec/features/widget_management_spec.rb`
|
35
|
+
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
|
@@ -0,0 +1,38 @@
|
|
1
|
+
Feature: URL helpers in mailer examples
|
2
|
+
|
3
|
+
Scenario: using URL helpers with default options
|
4
|
+
Given a file named "config/initializers/mailer_defaults.rb" with:
|
5
|
+
"""ruby
|
6
|
+
Rails.configuration.action_mailer.default_url_options = { :host => 'example.com' }
|
7
|
+
"""
|
8
|
+
And a file named "spec/mailers/notifications_spec.rb" with:
|
9
|
+
"""ruby
|
10
|
+
require 'spec_helper'
|
11
|
+
|
12
|
+
describe Notifications do
|
13
|
+
it 'should have access to URL helpers' do
|
14
|
+
expect { gadgets_url }.not_to raise_error
|
15
|
+
end
|
16
|
+
end
|
17
|
+
"""
|
18
|
+
When I run `rspec spec`
|
19
|
+
Then the examples should all pass
|
20
|
+
|
21
|
+
Scenario: using URL helpers without default options
|
22
|
+
Given a file named "config/initializers/mailer_defaults.rb" with:
|
23
|
+
"""ruby
|
24
|
+
# no default options
|
25
|
+
"""
|
26
|
+
And a file named "spec/mailers/notifications_spec.rb" with:
|
27
|
+
"""ruby
|
28
|
+
require 'spec_helper'
|
29
|
+
|
30
|
+
describe Notifications do
|
31
|
+
it 'should have access to URL helpers' do
|
32
|
+
expect { gadgets_url :host => 'example.com' }.not_to raise_error
|
33
|
+
expect { gadgets_url }.to raise_error
|
34
|
+
end
|
35
|
+
end
|
36
|
+
"""
|
37
|
+
When I run `rspec spec`
|
38
|
+
Then the examples should all pass
|
@@ -0,0 +1,18 @@
|
|
1
|
+
rspec-rails offers a number of custom matchers, most of which are
|
2
|
+
rspec-compatible wrappers for Rails' assertions.
|
3
|
+
|
4
|
+
### redirects
|
5
|
+
|
6
|
+
# delegates to assert_redirected_to
|
7
|
+
expect(response).to redirect_to(path)
|
8
|
+
|
9
|
+
### templates
|
10
|
+
|
11
|
+
# delegates to assert_template
|
12
|
+
expect(response).to render_template(template_name)
|
13
|
+
|
14
|
+
### assigned objects
|
15
|
+
|
16
|
+
# passes if assigns(:widget) is an instance of Widget
|
17
|
+
# and it is not persisted
|
18
|
+
expect(assigns(:widget)).to be_a_new(Widget)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
Feature: be_a_new matcher
|
2
|
+
|
3
|
+
The `be_a_new` matcher accepts a class and passes if the subject is an
|
4
|
+
instance of that class that returns false to persisted?
|
5
|
+
|
6
|
+
You can also chain `with` on `be_a_new` with a hash of attributes to specify
|
7
|
+
the subject has equal attributes.
|
8
|
+
|
9
|
+
Scenario: example spec with four be_a_new possibilities
|
10
|
+
Given a file named "spec/models/widget_spec.rb" with:
|
11
|
+
"""ruby
|
12
|
+
require "spec_helper"
|
13
|
+
|
14
|
+
describe Widget do
|
15
|
+
context "when initialized" do
|
16
|
+
subject(:widget) { Widget.new }
|
17
|
+
|
18
|
+
it "is a new widget" do
|
19
|
+
expect(widget).to be_a_new(Widget)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "is not a new string" do
|
23
|
+
expect(widget).not_to be_a_new(String)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when saved" do
|
28
|
+
subject(:widget) { Widget.create }
|
29
|
+
|
30
|
+
it "is not a new widget" do
|
31
|
+
expect(widget).not_to be_a_new(Widget)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "is not a new string" do
|
35
|
+
expect(widget).not_to be_a_new(String)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
"""
|
40
|
+
When I run `rspec spec/models/widget_spec.rb`
|
41
|
+
Then the examples should all pass
|
@@ -0,0 +1,40 @@
|
|
1
|
+
Feature: redirect_to matcher
|
2
|
+
|
3
|
+
The `redirect_to` matcher is used to specify that a request redirects to a
|
4
|
+
given template or action. It delegates to
|
5
|
+
[`assert_redirected_to`](http://api.rubyonrails.org/classes/ActionDispatch/Assertions/ResponseAssertions.html#method-i-assert_redirected_to).
|
6
|
+
|
7
|
+
It is available in controller specs (spec/controllers) and request
|
8
|
+
specs (spec/requests).
|
9
|
+
|
10
|
+
Scenario: redirect_to with four possible options
|
11
|
+
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
|
12
|
+
"""ruby
|
13
|
+
require "spec_helper"
|
14
|
+
|
15
|
+
describe WidgetsController do
|
16
|
+
|
17
|
+
describe "#create" do
|
18
|
+
subject { post :create, :widget => { :name => "Foo" } }
|
19
|
+
|
20
|
+
it "redirects to widget_url(@widget)" do
|
21
|
+
expect(subject).to redirect_to(widget_url(assigns(:widget)))
|
22
|
+
end
|
23
|
+
|
24
|
+
it "redirects_to :action => :show" do
|
25
|
+
expect(subject).to redirect_to :action => :show,
|
26
|
+
:id => assigns(:widget).id
|
27
|
+
end
|
28
|
+
|
29
|
+
it "redirects_to(@widget)" do
|
30
|
+
expect(subject).to redirect_to(assigns(:widget))
|
31
|
+
end
|
32
|
+
|
33
|
+
it "redirects_to /widgets/:id" do
|
34
|
+
expect(subject).to redirect_to("/widgets/#{assigns(:widget).id}")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
"""
|
39
|
+
When I run `rspec spec/controllers/widgets_controller_spec.rb`
|
40
|
+
Then the examples should all pass
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Feature: ActiveRecord::Relation match array
|
2
|
+
|
3
|
+
The `match_array` matcher can be used with an `ActiveRecord::Relation`
|
4
|
+
(scope). The assertion will pass if the scope would return all of the
|
5
|
+
elements specified in the array on the right hand side.
|
6
|
+
|
7
|
+
Scenario: example spec with relation match_array matcher
|
8
|
+
Given a file named "spec/models/widget_spec.rb" with:
|
9
|
+
"""ruby
|
10
|
+
require "spec_helper"
|
11
|
+
|
12
|
+
describe Widget do
|
13
|
+
let!(:widgets) { Array.new(3) { Widget.create } }
|
14
|
+
|
15
|
+
if ::Rails::VERSION::STRING >= '4'
|
16
|
+
subject { Widget.all }
|
17
|
+
else
|
18
|
+
subject { Widget.scoped }
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns all widgets in any order" do
|
22
|
+
expect(subject).to match_array(widgets)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
"""
|
26
|
+
When I run `rspec spec/models/widget_spec.rb`
|
27
|
+
Then the examples should all pass
|
@@ -0,0 +1,49 @@
|
|
1
|
+
Feature: render_template matcher
|
2
|
+
|
3
|
+
The `render_template` matcher is used to specify that a request renders a
|
4
|
+
given template. It delegates to
|
5
|
+
[`assert_template`](http://api.rubyonrails.org/classes/ActionController/TemplateAssertions.html#method-i-assert_template)
|
6
|
+
|
7
|
+
It is available in controller specs (spec/controllers) and request
|
8
|
+
specs (spec/requests).
|
9
|
+
|
10
|
+
NOTE: use `redirect_to(:action => 'new')` for redirects, not `render_template`.
|
11
|
+
|
12
|
+
Scenario: render_template with three possible options
|
13
|
+
Given a file named "spec/controllers/gadgets_spec.rb" with:
|
14
|
+
"""ruby
|
15
|
+
require "spec_helper"
|
16
|
+
|
17
|
+
describe GadgetsController do
|
18
|
+
describe "GET #index" do
|
19
|
+
subject { get :index }
|
20
|
+
|
21
|
+
it "renders the index template" do
|
22
|
+
expect(subject).to render_template(:index)
|
23
|
+
expect(subject).to render_template("index")
|
24
|
+
expect(subject).to render_template("gadgets/index")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
"""
|
29
|
+
When I run `rspec spec/controllers/gadgets_spec.rb`
|
30
|
+
Then the examples should all pass
|
31
|
+
|
32
|
+
Scenario: render_template in a view spec
|
33
|
+
Given a file named "spec/views/gadgets/index.html.erb_spec.rb" with:
|
34
|
+
"""ruby
|
35
|
+
require "spec_helper"
|
36
|
+
|
37
|
+
describe "gadgets/index" do
|
38
|
+
it "renders the index template" do
|
39
|
+
assign(:gadgets, [stub_model(Gadget)])
|
40
|
+
render
|
41
|
+
|
42
|
+
expect(view).to render_template(:index)
|
43
|
+
expect(view).to render_template("index")
|
44
|
+
expect(view).to render_template("gadgets/index")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
"""
|
48
|
+
When I run `rspec spec/views`
|
49
|
+
Then the examples should all pass
|
@@ -0,0 +1,147 @@
|
|
1
|
+
Feature: mock_model
|
2
|
+
|
3
|
+
The `mock_model` method generates a test double that acts like an instance of
|
4
|
+
`ActiveModel`. This is different from the `stub_model` method which generates
|
5
|
+
an instance of a real model class.
|
6
|
+
|
7
|
+
The benefit of `mock_model` over `stub_model` is that it is a true double, so
|
8
|
+
the examples are not dependent on the behavior (or mis-behavior), or even the
|
9
|
+
existence of any other code. If you're working on a controller spec and you
|
10
|
+
need a model that doesn't exist, you can pass `mock_model` a string and the
|
11
|
+
generated object will act as though its an instance of the class named by
|
12
|
+
that string.
|
13
|
+
|
14
|
+
Scenario: passing a string that represents a non-existent constant
|
15
|
+
Given a file named "spec/models/car_spec.rb" with:
|
16
|
+
"""ruby
|
17
|
+
require "spec_helper"
|
18
|
+
|
19
|
+
describe "mock_model('Car') with no Car constant in existence" do
|
20
|
+
it "generates a constant" do
|
21
|
+
expect(Object.const_defined?(:Car)).to be_falsey
|
22
|
+
mock_model("Car")
|
23
|
+
expect(Object.const_defined?(:Car)).to be_truthy
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "generates an object that ..." do
|
27
|
+
it "returns the correct name" do
|
28
|
+
car = mock_model("Car")
|
29
|
+
expect(car.class.name).to eq("Car")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "says it is a Car" do
|
33
|
+
car = mock_model("Car")
|
34
|
+
expect(car).to be_a(Car)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
"""
|
39
|
+
When I run `rspec spec/models/car_spec.rb`
|
40
|
+
Then the examples should all pass
|
41
|
+
|
42
|
+
Scenario: passing a string that represents an existing constant
|
43
|
+
Given a file named "spec/models/widget_spec.rb" with:
|
44
|
+
"""ruby
|
45
|
+
require "spec_helper"
|
46
|
+
|
47
|
+
describe Widget do
|
48
|
+
it "uses the existing constant" do
|
49
|
+
widget = mock_model("Widget")
|
50
|
+
expect(widget).to be_a(Widget)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
"""
|
54
|
+
When I run `rspec spec/models/widget_spec.rb`
|
55
|
+
Then the examples should all pass
|
56
|
+
|
57
|
+
Scenario: passing a class that does not extend ActiveModel::Naming
|
58
|
+
Given a file named "spec/models/string_spec.rb" with:
|
59
|
+
"""ruby
|
60
|
+
require "spec_helper"
|
61
|
+
|
62
|
+
describe String do
|
63
|
+
it "raises" do
|
64
|
+
expect { mock_model(String) }.to raise_exception
|
65
|
+
end
|
66
|
+
end
|
67
|
+
"""
|
68
|
+
When I run `rspec spec/models/string_spec.rb`
|
69
|
+
Then the examples should all pass
|
70
|
+
|
71
|
+
Scenario: passing an Active Record constant
|
72
|
+
Given a file named "spec/models/widget_spec.rb" with:
|
73
|
+
"""ruby
|
74
|
+
require "spec_helper"
|
75
|
+
|
76
|
+
describe Widget do
|
77
|
+
let(:widget) { mock_model(Widget) }
|
78
|
+
|
79
|
+
it "is valid by default" do
|
80
|
+
expect(widget).to be_valid
|
81
|
+
end
|
82
|
+
|
83
|
+
it "is not a new record by default" do
|
84
|
+
expect(widget).not_to be_new_record
|
85
|
+
end
|
86
|
+
|
87
|
+
it "can be converted to a new record" do
|
88
|
+
expect(widget.as_new_record).to be_new_record
|
89
|
+
end
|
90
|
+
|
91
|
+
it "sets :id to nil upon destroy" do
|
92
|
+
widget.destroy
|
93
|
+
expect(widget.id).to be_nil
|
94
|
+
end
|
95
|
+
end
|
96
|
+
"""
|
97
|
+
When I run `rspec spec/models/widget_spec.rb`
|
98
|
+
Then the examples should all pass
|
99
|
+
|
100
|
+
Scenario: passing an Active Record constant with method stubs
|
101
|
+
Given a file named "spec/models/widget_spec.rb" with:
|
102
|
+
"""ruby
|
103
|
+
require "spec_helper"
|
104
|
+
|
105
|
+
describe "mock_model(Widget) with stubs" do
|
106
|
+
let(:widget) do
|
107
|
+
mock_model Widget, :foo => "bar",
|
108
|
+
:save => true,
|
109
|
+
:update_attributes => false
|
110
|
+
end
|
111
|
+
|
112
|
+
it "supports stubs for methods that don't exist in ActiveModel or ActiveRecord" do
|
113
|
+
expect(widget.foo).to eq("bar")
|
114
|
+
end
|
115
|
+
|
116
|
+
it "supports stubs for methods that do exist" do
|
117
|
+
expect(widget.save).to eq(true)
|
118
|
+
expect(widget.update_attributes).to be_falsey
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "#errors" do
|
122
|
+
context "with update_attributes => false" do
|
123
|
+
it "is not empty" do
|
124
|
+
expect(widget.errors).not_to be_empty
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
"""
|
130
|
+
When I run `rspec spec/models/widget_spec.rb`
|
131
|
+
Then the examples should all pass
|
132
|
+
|
133
|
+
Scenario: mock_model outside rails
|
134
|
+
Given a file named "mock_model_outside_rails_spec.rb" with:
|
135
|
+
"""ruby
|
136
|
+
require 'rspec/rails/mocks'
|
137
|
+
|
138
|
+
describe "Foo" do
|
139
|
+
it "is mockable" do
|
140
|
+
foo = mock_model("Foo")
|
141
|
+
expect(foo.id).to eq(1001)
|
142
|
+
expect(foo.to_param).to eq("1001")
|
143
|
+
end
|
144
|
+
end
|
145
|
+
"""
|
146
|
+
When I run `rspec mock_model_outside_rails_spec.rb`
|
147
|
+
Then the examples should all pass
|