rspec-rails 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +2 -1
- data/Capybara.md +1 -3
- data/Changelog.md +126 -58
- data/README.md +31 -4
- data/lib/generators/rspec/controller/templates/controller_spec.rb +1 -1
- data/lib/generators/rspec/controller/templates/view_spec.rb +1 -1
- data/lib/generators/rspec/feature/templates/feature_spec.rb +1 -1
- data/lib/generators/rspec/helper/templates/helper_spec.rb +1 -1
- data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +21 -1
- data/lib/generators/rspec/integration/templates/request_spec.rb +1 -1
- data/lib/generators/rspec/mailer/templates/mailer_spec.rb +1 -1
- data/lib/generators/rspec/model/templates/model_spec.rb +1 -1
- data/lib/generators/rspec/observer/templates/observer_spec.rb +1 -1
- data/lib/generators/rspec/scaffold/templates/controller_spec.rb +11 -3
- data/lib/generators/rspec/scaffold/templates/edit_spec.rb +4 -3
- data/lib/generators/rspec/scaffold/templates/index_spec.rb +2 -2
- data/lib/generators/rspec/scaffold/templates/new_spec.rb +5 -4
- data/lib/generators/rspec/scaffold/templates/routing_spec.rb +1 -1
- data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
- data/lib/generators/rspec/view/templates/view_spec.rb +1 -1
- data/lib/rspec/rails.rb +1 -8
- data/lib/rspec/rails/configuration.rb +72 -0
- data/lib/rspec/rails/example.rb +0 -64
- data/lib/rspec/rails/example/controller_example_group.rb +16 -12
- data/lib/rspec/rails/example/feature_example_group.rb +0 -2
- data/lib/rspec/rails/example/helper_example_group.rb +0 -2
- data/lib/rspec/rails/example/mailer_example_group.rb +0 -1
- data/lib/rspec/rails/example/model_example_group.rb +0 -4
- data/lib/rspec/rails/example/request_example_group.rb +0 -2
- data/lib/rspec/rails/example/routing_example_group.rb +0 -2
- data/lib/rspec/rails/example/view_example_group.rb +1 -2
- data/lib/rspec/rails/extensions.rb +0 -1
- data/lib/rspec/rails/fixture_support.rb +0 -8
- data/lib/rspec/rails/matchers.rb +1 -0
- data/lib/rspec/rails/matchers/have_http_status.rb +355 -0
- data/lib/rspec/rails/matchers/routing_matchers.rb +1 -1
- data/lib/rspec/rails/tasks/rspec.rake +12 -5
- data/lib/rspec/rails/version.rb +1 -1
- data/lib/rspec/rails/view_rendering.rb +6 -34
- metadata +42 -316
- metadata.gz.sig +0 -0
- data/features/Generators.md +0 -25
- data/features/GettingStarted.md +0 -84
- data/features/README.md +0 -48
- data/features/RailsVersions.md +0 -4
- data/features/Transactions.md +0 -84
- data/features/Upgrade.md +0 -121
- data/features/controller_specs/Cookies.md +0 -57
- data/features/controller_specs/README.md +0 -45
- data/features/controller_specs/anonymous_controller.feature +0 -436
- data/features/controller_specs/bypass_rescue.feature +0 -75
- data/features/controller_specs/controller_spec.feature +0 -58
- data/features/controller_specs/engine_routes.feature +0 -51
- data/features/controller_specs/isolation_from_views.feature +0 -87
- data/features/controller_specs/render_views.feature +0 -114
- data/features/directory_structure.feature +0 -71
- data/features/feature_specs/feature_spec.feature +0 -35
- data/features/helper_specs/helper_spec.feature +0 -122
- data/features/mailer_specs/url_helpers.feature +0 -38
- data/features/matchers/README.md +0 -18
- data/features/matchers/new_record_matcher.feature +0 -41
- data/features/matchers/redirect_to_matcher.feature +0 -40
- data/features/matchers/relation_match_array.feature +0 -27
- data/features/matchers/render_template_matcher.feature +0 -49
- data/features/mocks/mock_model.feature +0 -147
- data/features/mocks/stub_model.feature +0 -58
- data/features/model_specs/README.md +0 -21
- data/features/model_specs/errors_on.feature +0 -51
- data/features/model_specs/records.feature +0 -27
- data/features/model_specs/transactional_examples.feature +0 -109
- data/features/request_specs/request_spec.feature +0 -49
- data/features/routing_specs/README.md +0 -16
- data/features/routing_specs/be_routable_matcher.feature +0 -80
- data/features/routing_specs/engine_routes.feature +0 -38
- data/features/routing_specs/named_routes.feature +0 -18
- data/features/routing_specs/route_to_matcher.feature +0 -90
- data/features/step_definitions/additional_cli_steps.rb +0 -4
- data/features/step_definitions/model_steps.rb +0 -3
- data/features/support/capybara.rb +0 -7
- data/features/support/env.rb +0 -53
- data/features/support/rails_versions.rb +0 -4
- data/features/support/rubinius.rb +0 -6
- data/features/view_specs/inferred_controller_path.feature +0 -45
- data/features/view_specs/stub_template.feature +0 -51
- data/features/view_specs/view_spec.feature +0 -206
- data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
- data/lib/rspec/rails/mocks.rb +0 -272
- data/lib/rspec/rails/vendor/webrat.rb +0 -33
- data/spec/generators/rspec/controller/controller_generator_spec.rb +0 -97
- data/spec/generators/rspec/feature/feature_generator_spec.rb +0 -43
- data/spec/generators/rspec/helper/helper_generator_spec.rb +0 -30
- data/spec/generators/rspec/install/install_generator_spec.rb +0 -30
- data/spec/generators/rspec/integration/integration_generator_spec.rb +0 -32
- data/spec/generators/rspec/mailer/mailer_generator_spec.rb +0 -48
- data/spec/generators/rspec/model/model_generator_spec.rb +0 -52
- data/spec/generators/rspec/observer/observer_generator_spec.rb +0 -21
- data/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +0 -138
- data/spec/generators/rspec/view/view_generator_spec.rb +0 -41
- data/spec/rspec/rails/assertion_adapter_spec.rb +0 -28
- data/spec/rspec/rails/assertion_delegator_spec.rb +0 -43
- data/spec/rspec/rails/configuration_spec.rb +0 -26
- data/spec/rspec/rails/deprecations_spec.rb +0 -18
- data/spec/rspec/rails/example/controller_example_group_spec.rb +0 -159
- data/spec/rspec/rails/example/feature_example_group_spec.rb +0 -56
- data/spec/rspec/rails/example/helper_example_group_spec.rb +0 -66
- data/spec/rspec/rails/example/mailer_example_group_spec.rb +0 -21
- data/spec/rspec/rails/example/model_example_group_spec.rb +0 -15
- data/spec/rspec/rails/example/request_example_group_spec.rb +0 -17
- data/spec/rspec/rails/example/routing_example_group_spec.rb +0 -32
- data/spec/rspec/rails/example/view_example_group_spec.rb +0 -235
- data/spec/rspec/rails/extensions/active_model/errors_on_spec.rb +0 -23
- data/spec/rspec/rails/extensions/active_record/base_spec.rb +0 -42
- data/spec/rspec/rails/fixture_support_spec.rb +0 -17
- data/spec/rspec/rails/matchers/be_a_new_spec.rb +0 -142
- data/spec/rspec/rails/matchers/be_new_record_spec.rb +0 -33
- data/spec/rspec/rails/matchers/be_routable_spec.rb +0 -41
- data/spec/rspec/rails/matchers/be_valid_spec.rb +0 -73
- data/spec/rspec/rails/matchers/has_spec.rb +0 -29
- data/spec/rspec/rails/matchers/have_rendered_spec.rb +0 -93
- data/spec/rspec/rails/matchers/redirect_to_spec.rb +0 -81
- data/spec/rspec/rails/matchers/relation_match_array_spec.rb +0 -31
- data/spec/rspec/rails/matchers/route_to_spec.rb +0 -151
- data/spec/rspec/rails/minitest_lifecycle_adapter_spec.rb +0 -31
- data/spec/rspec/rails/mocks/mock_model_spec.rb +0 -400
- data/spec/rspec/rails/mocks/stub_model_spec.rb +0 -154
- data/spec/rspec/rails/setup_and_teardown_adapter_spec.rb +0 -32
- data/spec/rspec/rails/view_rendering_spec.rb +0 -111
- data/spec/spec_helper.rb +0 -33
- data/spec/support/ar_classes.rb +0 -42
- data/spec/support/helpers.rb +0 -34
- data/spec/support/matchers.rb +0 -9
- data/spec/support/null_object.rb +0 -6
@@ -1,81 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
require "active_support"
|
3
|
-
require "active_support/test_case"
|
4
|
-
|
5
|
-
describe "redirect_to" do
|
6
|
-
include RSpec::Rails::Matchers::RedirectTo
|
7
|
-
|
8
|
-
let(:response) { ActionController::TestResponse.new }
|
9
|
-
|
10
|
-
context "with should" do
|
11
|
-
context "when assert_redirected_to passes" do
|
12
|
-
def assert_redirected_to(*); end
|
13
|
-
|
14
|
-
it "passes" do
|
15
|
-
expect do
|
16
|
-
expect(response).to redirect_to("destination")
|
17
|
-
end.to_not raise_exception
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context "when assert_redirected_to fails" do
|
22
|
-
def assert_redirected_to(*)
|
23
|
-
raise ActiveSupport::TestCase::Assertion.new("this message")
|
24
|
-
end
|
25
|
-
|
26
|
-
it "uses failure message from assert_redirected_to" do
|
27
|
-
expect do
|
28
|
-
expect(response).to redirect_to("destination")
|
29
|
-
end.to raise_exception("this message")
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context "when fails due to some other exception" do
|
34
|
-
def assert_redirected_to(*)
|
35
|
-
raise "oops"
|
36
|
-
end
|
37
|
-
|
38
|
-
it "raises that exception" do
|
39
|
-
expect do
|
40
|
-
expect(response).to redirect_to("destination")
|
41
|
-
end.to raise_exception("oops")
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context "with should_not" do
|
47
|
-
context "when assert_redirected_to fails" do
|
48
|
-
def assert_redirected_to(*)
|
49
|
-
raise ActiveSupport::TestCase::Assertion.new("this message")
|
50
|
-
end
|
51
|
-
|
52
|
-
it "passes" do
|
53
|
-
expect do
|
54
|
-
expect(response).not_to redirect_to("destination")
|
55
|
-
end.to_not raise_exception
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
context "when assert_redirected_to passes" do
|
60
|
-
def assert_redirected_to(*); end
|
61
|
-
|
62
|
-
it "fails with custom failure message" do
|
63
|
-
expect do
|
64
|
-
expect(response).not_to redirect_to("destination")
|
65
|
-
end.to raise_exception(/expected not to redirect to \"destination\", but did/)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
context "when fails due to some other exception" do
|
70
|
-
def assert_redirected_to(*)
|
71
|
-
raise "oops"
|
72
|
-
end
|
73
|
-
|
74
|
-
it "raises that exception" do
|
75
|
-
expect do
|
76
|
-
expect(response).not_to redirect_to("destination")
|
77
|
-
end.to raise_exception("oops")
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "ActiveSupport::Relation match_array matcher" do
|
4
|
-
before { MockableModel.delete_all }
|
5
|
-
|
6
|
-
let!(:models) { Array.new(3) { MockableModel.create } }
|
7
|
-
|
8
|
-
if ::Rails::VERSION::STRING >= '4'
|
9
|
-
it "verifies that the scope returns the records on the right hand side, regardless of order" do
|
10
|
-
expect(MockableModel.all).to match_array(models.reverse)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "fails if the scope encompasses more records than on the right hand side" do
|
14
|
-
MockableModel.create
|
15
|
-
expect(MockableModel.all).not_to match_array(models.reverse)
|
16
|
-
end
|
17
|
-
else
|
18
|
-
it "verifies that the scope returns the records on the right hand side, regardless of order" do
|
19
|
-
expect(MockableModel.scoped).to match_array(models.reverse)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "fails if the scope encompasses more records than on the right hand side" do
|
23
|
-
MockableModel.create
|
24
|
-
expect(MockableModel.scoped).not_to match_array(models.reverse)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
it "fails if the scope encompasses fewer records than on the right hand side" do
|
29
|
-
expect(MockableModel.limit(models.length - 1)).not_to match_array(models.reverse)
|
30
|
-
end
|
31
|
-
end
|
@@ -1,151 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "route_to" do
|
4
|
-
include RSpec::Rails::Matchers::RoutingMatchers
|
5
|
-
include RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers
|
6
|
-
|
7
|
-
def assert_recognizes(*)
|
8
|
-
# no-op
|
9
|
-
end
|
10
|
-
|
11
|
-
it "provides a description" do
|
12
|
-
matcher = route_to("these" => "options")
|
13
|
-
matcher.matches?(:get => "path")
|
14
|
-
expect(matcher.description).to eq("route {:get=>\"path\"} to {\"these\"=>\"options\"}")
|
15
|
-
end
|
16
|
-
|
17
|
-
it "delegates to assert_recognizes" do
|
18
|
-
expect(self).to receive(:assert_recognizes).with({ "these" => "options" }, { :method=> :get, :path=>"path" }, {})
|
19
|
-
expect({:get => "path"}).to route_to("these" => "options")
|
20
|
-
end
|
21
|
-
|
22
|
-
context "with shortcut syntax" do
|
23
|
-
it "routes with extra options" do
|
24
|
-
expect(self).to receive(:assert_recognizes).with({ :controller => "controller", :action => "action", :extra => "options"}, { :method=> :get, :path=>"path" }, {})
|
25
|
-
expect(get("path")).to route_to("controller#action", :extra => "options")
|
26
|
-
end
|
27
|
-
|
28
|
-
it "routes without extra options" do
|
29
|
-
expect(self).to receive(:assert_recognizes).with(
|
30
|
-
{:controller => "controller", :action => "action"},
|
31
|
-
{:method=> :get, :path=>"path" },
|
32
|
-
{}
|
33
|
-
)
|
34
|
-
expect(get("path")).to route_to("controller#action")
|
35
|
-
end
|
36
|
-
|
37
|
-
it "routes with one query parameter" do
|
38
|
-
expect(self).to receive(:assert_recognizes).with(
|
39
|
-
{:controller => "controller", :action => "action", :queryitem => "queryvalue"},
|
40
|
-
{:method=> :get, :path=>"path" },
|
41
|
-
{'queryitem' => 'queryvalue' }
|
42
|
-
)
|
43
|
-
expect(get("path?queryitem=queryvalue")).to route_to("controller#action", :queryitem => 'queryvalue')
|
44
|
-
end
|
45
|
-
|
46
|
-
it "routes with multiple query parameters" do
|
47
|
-
expect(self).to receive(:assert_recognizes).with(
|
48
|
-
{:controller => "controller", :action => "action", :queryitem => "queryvalue", :qi2 => 'qv2'},
|
49
|
-
{:method=> :get, :path=>"path"},
|
50
|
-
{'queryitem' => 'queryvalue', 'qi2' => 'qv2'}
|
51
|
-
)
|
52
|
-
expect(get("path?queryitem=queryvalue&qi2=qv2")).to route_to("controller#action", :queryitem => 'queryvalue', :qi2 => 'qv2')
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
context "with should" do
|
58
|
-
context "when assert_recognizes passes" do
|
59
|
-
it "passes" do
|
60
|
-
expect do
|
61
|
-
expect({:get => "path"}).to route_to("these" => "options")
|
62
|
-
end.to_not raise_exception
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context "when assert_recognizes fails with an assertion failure" do
|
67
|
-
it "fails with message from assert_recognizes" do
|
68
|
-
def assert_recognizes(*)
|
69
|
-
raise ActiveSupport::TestCase::Assertion.new("this message")
|
70
|
-
end
|
71
|
-
expect do
|
72
|
-
expect({:get => "path"}).to route_to("these" => "options")
|
73
|
-
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "this message")
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
context "when assert_recognizes fails with a routing error" do
|
78
|
-
it "fails with message from assert_recognizes" do
|
79
|
-
def assert_recognizes(*)
|
80
|
-
raise ActionController::RoutingError.new("this message")
|
81
|
-
end
|
82
|
-
expect do
|
83
|
-
expect({:get => "path"}).to route_to("these" => "options")
|
84
|
-
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "this message")
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context "when an exception is raised" do
|
89
|
-
it "raises that exception" do
|
90
|
-
def assert_recognizes(*)
|
91
|
-
raise "oops"
|
92
|
-
end
|
93
|
-
expect do
|
94
|
-
expect({:get => "path"}).to route_to("these" => "options")
|
95
|
-
end.to raise_exception("oops")
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
context "with should_not" do
|
101
|
-
context "when assert_recognizes passes" do
|
102
|
-
it "fails with custom message" do
|
103
|
-
expect do
|
104
|
-
expect({:get => "path"}).not_to route_to("these" => "options")
|
105
|
-
end.to raise_error(/expected {:get=>"path"} not to route to {"these"=>"options"}/)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
context "when assert_recognizes fails with an assertion failure" do
|
110
|
-
it "passes" do
|
111
|
-
def assert_recognizes(*)
|
112
|
-
raise ActiveSupport::TestCase::Assertion.new("this message")
|
113
|
-
end
|
114
|
-
expect do
|
115
|
-
expect({:get => "path"}).not_to route_to("these" => "options")
|
116
|
-
end.to_not raise_error
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
context "when assert_recognizes fails with a routing error" do
|
121
|
-
it "passes" do
|
122
|
-
def assert_recognizes(*)
|
123
|
-
raise ActionController::RoutingError.new("this message")
|
124
|
-
end
|
125
|
-
expect do
|
126
|
-
expect({:get => "path"}).not_to route_to("these" => "options")
|
127
|
-
end.to_not raise_error
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
context "when an exception is raised" do
|
132
|
-
it "raises that exception" do
|
133
|
-
def assert_recognizes(*)
|
134
|
-
raise "oops"
|
135
|
-
end
|
136
|
-
expect do
|
137
|
-
expect({:get => "path"}).not_to route_to("these" => "options")
|
138
|
-
end.to raise_exception("oops")
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
it "uses failure message from assert_recognizes" do
|
144
|
-
def assert_recognizes(*)
|
145
|
-
raise ActiveSupport::TestCase::Assertion, "this message"
|
146
|
-
end
|
147
|
-
expect do
|
148
|
-
expect({"this" => "path"}).to route_to("these" => "options")
|
149
|
-
end.to raise_error("this message")
|
150
|
-
end
|
151
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe RSpec::Rails::MinitestLifecycleAdapter do
|
4
|
-
it "invokes minitest lifecycle hooks at the appropriate times" do
|
5
|
-
invocations = []
|
6
|
-
example_group = RSpec::Core::ExampleGroup.describe("MinitestHooks") do
|
7
|
-
include RSpec::Rails::MinitestLifecycleAdapter
|
8
|
-
|
9
|
-
define_method(:before_setup) { invocations << :before_setup }
|
10
|
-
define_method(:after_setup) { invocations << :after_setup }
|
11
|
-
define_method(:before_teardown) { invocations << :before_teardown }
|
12
|
-
define_method(:after_teardown) { invocations << :after_teardown }
|
13
|
-
end
|
14
|
-
|
15
|
-
example = example_group.example("foo") { invocations << :example }
|
16
|
-
example_group.run(NullObject.new)
|
17
|
-
|
18
|
-
expect(invocations).to eq([
|
19
|
-
:before_setup, :after_setup, :example, :before_teardown, :after_teardown
|
20
|
-
])
|
21
|
-
end
|
22
|
-
|
23
|
-
it "allows let variables named 'send'" do
|
24
|
-
run_result = ::RSpec::Core::ExampleGroup.describe do
|
25
|
-
let(:send) { "WHAT" }
|
26
|
-
specify { expect(send).to eq "WHAT" }
|
27
|
-
end.run NullObject.new
|
28
|
-
|
29
|
-
expect(run_result).to be true
|
30
|
-
end
|
31
|
-
end
|
@@ -1,400 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "mock_model(RealModel)" do
|
4
|
-
context "given a String" do
|
5
|
-
context "that does not represent an existing constant" do
|
6
|
-
it "class says it's name" do
|
7
|
-
model = mock_model("Foo")
|
8
|
-
expect(model.class.name).to eq("Foo")
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
context "that represents an existing constant" do
|
13
|
-
context "that extends ActiveModel::Naming" do
|
14
|
-
it "treats the constant as the class" do
|
15
|
-
model = mock_model("MockableModel")
|
16
|
-
expect(model.class.name).to eq("MockableModel")
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context "that does not extend ActiveModel::Naming" do
|
21
|
-
it "raises with a helpful message" do
|
22
|
-
expect do
|
23
|
-
mock_model("String")
|
24
|
-
end.to raise_error(ArgumentError)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context "given a class that does not extend ActiveModel::Naming" do
|
31
|
-
it "raises with a helpful message" do
|
32
|
-
expect do
|
33
|
-
mock_model(String)
|
34
|
-
end.to raise_error(ArgumentError)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe "with #id stubbed" do
|
39
|
-
before(:each) do
|
40
|
-
@model = mock_model(MockableModel, :id => 1)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "is named using the stubbed id value" do
|
44
|
-
expect(@model.instance_variable_get(:@name)).to eq("MockableModel_1")
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe "destroy" do
|
49
|
-
it "sets persisted to false" do
|
50
|
-
model = mock_model(MockableModel)
|
51
|
-
model.destroy
|
52
|
-
expect(model).not_to be_persisted
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe "association" do
|
57
|
-
it "constructs a mock association object" do
|
58
|
-
model = mock_model(MockableModel)
|
59
|
-
expect(model.association(:association_name)).to be
|
60
|
-
end
|
61
|
-
|
62
|
-
it "returns a different association object for each association name" do
|
63
|
-
model = mock_model(MockableModel)
|
64
|
-
posts = model.association(:posts)
|
65
|
-
authors = model.association(:authors)
|
66
|
-
|
67
|
-
expect(posts).not_to equal(authors)
|
68
|
-
end
|
69
|
-
|
70
|
-
it "returns the same association model each time for the same association name" do
|
71
|
-
model = mock_model(MockableModel)
|
72
|
-
posts1 = model.association(:posts)
|
73
|
-
posts2 = model.association(:posts)
|
74
|
-
|
75
|
-
expect(posts1).to equal(posts2)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "errors" do
|
80
|
-
context "default" do
|
81
|
-
it "is empty" do
|
82
|
-
model = mock_model(MockableModel)
|
83
|
-
expect(model.errors).to be_empty
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
context "with :save => false" do
|
88
|
-
it "is not empty" do
|
89
|
-
model = mock_model(MockableModel, :save => false)
|
90
|
-
expect(model.errors).not_to be_empty
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
context "with :update_attributes => false" do
|
95
|
-
it "is not empty" do
|
96
|
-
model = mock_model(MockableModel, :save => false)
|
97
|
-
expect(model.errors).not_to be_empty
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe "with params" do
|
103
|
-
it "does not mutate its parameters" do
|
104
|
-
params = {:a => 'b'}
|
105
|
-
mock_model(MockableModel, params)
|
106
|
-
expect(params).to eq({:a => 'b'})
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
describe "as association" do
|
111
|
-
before(:each) do
|
112
|
-
@real = AssociatedModel.create!
|
113
|
-
@mock_model = mock_model(MockableModel)
|
114
|
-
@real.mockable_model = @mock_model
|
115
|
-
end
|
116
|
-
|
117
|
-
it "passes: associated_model == mock" do
|
118
|
-
expect(@mock_model).to eq(@real.mockable_model)
|
119
|
-
end
|
120
|
-
|
121
|
-
it "passes: mock == associated_model" do
|
122
|
-
expect(@real.mockable_model).to eq(@mock_model)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
describe "as association that doesn't exist yet" do
|
127
|
-
before(:each) do
|
128
|
-
@real = AssociatedModel.create!
|
129
|
-
@mock_model = mock_model("Other")
|
130
|
-
@real.nonexistent_model = @mock_model
|
131
|
-
end
|
132
|
-
|
133
|
-
it "passes: associated_model == mock" do
|
134
|
-
expect(@mock_model).to eq(@real.nonexistent_model)
|
135
|
-
end
|
136
|
-
|
137
|
-
it "passes: mock == associated_model" do
|
138
|
-
expect(@real.nonexistent_model).to eq(@mock_model)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
describe "#is_a?" do
|
143
|
-
before(:each) do
|
144
|
-
@model = mock_model(SubMockableModel)
|
145
|
-
end
|
146
|
-
|
147
|
-
it "says it is_a?(RealModel)" do
|
148
|
-
expect(@model.is_a?(SubMockableModel)).to be(true)
|
149
|
-
end
|
150
|
-
|
151
|
-
it "says it is_a?(OtherModel) if RealModel is an ancestors" do
|
152
|
-
expect(@model.is_a?(MockableModel)).to be(true)
|
153
|
-
end
|
154
|
-
|
155
|
-
it "can be stubbed" do
|
156
|
-
expect(mock_model(MockableModel, :is_a? => true).is_a?(:Foo)).to be_truthy
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
describe "#kind_of?" do
|
161
|
-
before(:each) do
|
162
|
-
@model = mock_model(SubMockableModel)
|
163
|
-
end
|
164
|
-
|
165
|
-
it "says it is kind_of? if RealModel is" do
|
166
|
-
expect(@model.kind_of?(SubMockableModel)).to be(true)
|
167
|
-
end
|
168
|
-
|
169
|
-
it "says it is kind_of? if RealModel's ancestor is" do
|
170
|
-
expect(@model.kind_of?(MockableModel)).to be(true)
|
171
|
-
end
|
172
|
-
|
173
|
-
it "can be stubbed" do
|
174
|
-
expect(mock_model(MockableModel, :kind_of? => true).kind_of?(:Foo)).to be_truthy
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
describe "#instance_of?" do
|
179
|
-
before(:each) do
|
180
|
-
@model = mock_model(SubMockableModel)
|
181
|
-
end
|
182
|
-
|
183
|
-
it "says it is instance_of? if RealModel is" do
|
184
|
-
expect(@model.instance_of?(SubMockableModel)).to be(true)
|
185
|
-
end
|
186
|
-
|
187
|
-
it "does not say it instance_of? if RealModel isn't, even if it's ancestor is" do
|
188
|
-
expect(@model.instance_of?(MockableModel)).to be(false)
|
189
|
-
end
|
190
|
-
|
191
|
-
it "can be stubbed" do
|
192
|
-
expect(mock_model(MockableModel, :instance_of? => true).instance_of?(:Foo)).to be_truthy
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
describe "#respond_to?" do
|
197
|
-
context "with an ActiveRecord model" do
|
198
|
-
before(:each) do
|
199
|
-
allow(MockableModel).to receive(:column_names).and_return(["column_a", "column_b"])
|
200
|
-
@model = mock_model(MockableModel)
|
201
|
-
end
|
202
|
-
|
203
|
-
it "accepts two arguments" do
|
204
|
-
expect do
|
205
|
-
@model.respond_to?("title_before_type_cast", false)
|
206
|
-
end.to_not raise_exception
|
207
|
-
end
|
208
|
-
|
209
|
-
context "without as_null_object" do
|
210
|
-
it "says it will respond_to?(key) if RealModel has the attribute 'key'" do
|
211
|
-
expect(@model.respond_to?("column_a")).to be(true)
|
212
|
-
end
|
213
|
-
it "stubs column accessor (with string)" do
|
214
|
-
@model.respond_to?("column_a")
|
215
|
-
expect(@model.column_a).to be_nil
|
216
|
-
end
|
217
|
-
it "stubs column accessor (with symbol)" do
|
218
|
-
@model.respond_to?(:column_a)
|
219
|
-
expect(@model.column_a).to be_nil
|
220
|
-
end
|
221
|
-
it "does not stub column accessor if already stubbed in declaration (with string)" do
|
222
|
-
model = mock_model(MockableModel, "column_a" => "a")
|
223
|
-
model.respond_to?("column_a")
|
224
|
-
expect(model.column_a).to eq("a")
|
225
|
-
end
|
226
|
-
it "does not stub column accessor if already stubbed in declaration (with symbol)" do
|
227
|
-
model = mock_model(MockableModel, :column_a => "a")
|
228
|
-
model.respond_to?("column_a")
|
229
|
-
expect(model.column_a).to eq("a")
|
230
|
-
end
|
231
|
-
it "does not stub column accessor if already stubbed after declaration (with string)" do
|
232
|
-
allow(@model).to receive_messages("column_a" => "a")
|
233
|
-
@model.respond_to?("column_a")
|
234
|
-
expect(@model.column_a).to eq("a")
|
235
|
-
end
|
236
|
-
it "does not stub column accessor if already stubbed after declaration (with symbol)" do
|
237
|
-
allow(@model).to receive_messages(:column_a => "a")
|
238
|
-
@model.respond_to?("column_a")
|
239
|
-
expect(@model.column_a).to eq("a")
|
240
|
-
end
|
241
|
-
it "says it will not respond_to?(key) if RealModel does not have the attribute 'key'" do
|
242
|
-
expect(@model.respond_to?("column_c")).to be(false)
|
243
|
-
end
|
244
|
-
it "says it will not respond_to?(xxx_before_type_cast)" do
|
245
|
-
expect(@model.respond_to?("title_before_type_cast")).to be(false)
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
context "with as_null_object" do
|
250
|
-
it "says it will respond_to?(key) if RealModel has the attribute 'key'" do
|
251
|
-
expect(@model.as_null_object.respond_to?("column_a")).to be(true)
|
252
|
-
end
|
253
|
-
it "says it will respond_to?(key) even if RealModel does not have the attribute 'key'" do
|
254
|
-
expect(@model.as_null_object.respond_to?("column_c")).to be(true)
|
255
|
-
end
|
256
|
-
it "says it will not respond_to?(xxx_before_type_cast)" do
|
257
|
-
expect(@model.as_null_object.respond_to?("title_before_type_cast")).to be(false)
|
258
|
-
end
|
259
|
-
it "returns self for any unprepared message" do
|
260
|
-
@model.as_null_object.tap do |x|
|
261
|
-
expect(x.non_existant_message).to be(@model)
|
262
|
-
end
|
263
|
-
end
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
context "with a non-ActiveRecord model" do
|
268
|
-
it "responds as normal" do
|
269
|
-
model = NonActiveRecordModel.new
|
270
|
-
expect(model).to respond_to(:to_param)
|
271
|
-
end
|
272
|
-
|
273
|
-
context "with as_null_object" do
|
274
|
-
it "says it will not respond_to?(xxx_before_type_cast)" do
|
275
|
-
model = NonActiveRecordModel.new.as_null_object
|
276
|
-
expect(model.respond_to?("title_before_type_cast")).to be(false)
|
277
|
-
end
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
it "can be stubbed" do
|
282
|
-
expect(mock_model(MockableModel, :respond_to? => true).respond_to?(:foo)).to be_truthy
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
describe "#class" do
|
287
|
-
it "returns the mocked model" do
|
288
|
-
expect(mock_model(MockableModel).class).to eq(MockableModel)
|
289
|
-
end
|
290
|
-
|
291
|
-
it "can be stubbed" do
|
292
|
-
expect(mock_model(MockableModel, :class => String).class).to be(String)
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
describe "#to_s" do
|
297
|
-
it "returns (model.name)_(model#to_param)" do
|
298
|
-
expect(mock_model(MockableModel).to_s).to eq("MockableModel_#{to_param}")
|
299
|
-
end
|
300
|
-
|
301
|
-
it "can be stubbed" do
|
302
|
-
expect(mock_model(MockableModel, :to_s => "this string").to_s).to eq("this string")
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
describe "#destroyed?" do
|
307
|
-
context "default" do
|
308
|
-
it "returns false" do
|
309
|
-
@model = mock_model(SubMockableModel)
|
310
|
-
expect(@model.destroyed?).to be(false)
|
311
|
-
end
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
describe "#marked_for_destruction?" do
|
316
|
-
context "default" do
|
317
|
-
it "returns false" do
|
318
|
-
@model = mock_model(SubMockableModel)
|
319
|
-
expect(@model.marked_for_destruction?).to be(false)
|
320
|
-
end
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
324
|
-
describe "#persisted?" do
|
325
|
-
context "with default identifier" do
|
326
|
-
it "returns true" do
|
327
|
-
expect(mock_model(MockableModel)).to be_persisted
|
328
|
-
end
|
329
|
-
end
|
330
|
-
|
331
|
-
context "with explicit identifier via :id" do
|
332
|
-
it "returns true" do
|
333
|
-
expect(mock_model(MockableModel, :id => 37)).to be_persisted
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
|
-
context "with id => nil" do
|
338
|
-
it "returns false" do
|
339
|
-
expect(mock_model(MockableModel, :id => nil)).not_to be_persisted
|
340
|
-
end
|
341
|
-
end
|
342
|
-
end
|
343
|
-
|
344
|
-
describe "#valid?" do
|
345
|
-
context "default" do
|
346
|
-
it "returns true" do
|
347
|
-
expect(mock_model(MockableModel)).to be_valid
|
348
|
-
end
|
349
|
-
end
|
350
|
-
|
351
|
-
context "stubbed with false" do
|
352
|
-
it "returns false" do
|
353
|
-
expect(mock_model(MockableModel, :valid? => false)).not_to be_valid
|
354
|
-
end
|
355
|
-
end
|
356
|
-
end
|
357
|
-
|
358
|
-
describe "#as_new_record" do
|
359
|
-
it "says it is a new record" do
|
360
|
-
m = mock_model(MockableModel)
|
361
|
-
expect(m.as_new_record).to be_new_record
|
362
|
-
end
|
363
|
-
|
364
|
-
it "says it is not persisted" do
|
365
|
-
m = mock_model(MockableModel)
|
366
|
-
expect(m.as_new_record).not_to be_persisted
|
367
|
-
end
|
368
|
-
|
369
|
-
it "has a nil id" do
|
370
|
-
expect(mock_model(MockableModel).as_new_record.id).to be(nil)
|
371
|
-
end
|
372
|
-
|
373
|
-
it "returns nil for #to_param" do
|
374
|
-
expect(mock_model(MockableModel).as_new_record.to_param).to be(nil)
|
375
|
-
end
|
376
|
-
end
|
377
|
-
|
378
|
-
describe "#blank?" do
|
379
|
-
it "is false" do
|
380
|
-
expect(mock_model(MockableModel)).not_to be_blank
|
381
|
-
end
|
382
|
-
end
|
383
|
-
|
384
|
-
describe "ActiveModel Lint tests" do
|
385
|
-
require 'active_model/lint'
|
386
|
-
include RSpec::Rails::MinitestAssertionAdapter
|
387
|
-
include ActiveModel::Lint::Tests
|
388
|
-
|
389
|
-
# to_s is to support ruby-1.9
|
390
|
-
ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
|
391
|
-
example m.gsub('_',' ') do
|
392
|
-
send m
|
393
|
-
end
|
394
|
-
end
|
395
|
-
|
396
|
-
def model
|
397
|
-
mock_model(MockableModel, :id => nil)
|
398
|
-
end
|
399
|
-
end
|
400
|
-
end
|