rspec-rails 2.99.0.beta1 → 2.99.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Changelog.md +18 -0
- data/features/controller_specs/capybara.feature +44 -0
- data/features/controller_specs/render_views.feature +1 -1
- data/lib/autotest/rails_rspec2.rb +1 -0
- data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +0 -1
- data/lib/generators/rspec/integration/integration_generator.rb +3 -2
- data/lib/generators/rspec/scaffold/scaffold_generator.rb +3 -2
- data/lib/rspec/rails/adapters.rb +12 -5
- data/lib/rspec/rails/example/controller_example_group.rb +2 -1
- data/lib/rspec/rails/example/view_example_group.rb +3 -0
- data/lib/rspec/rails/matchers/have_extension.rb +1 -0
- data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
- data/lib/rspec/rails/matchers/routing_matchers.rb +4 -0
- data/lib/rspec/rails/mocks.rb +22 -0
- data/lib/rspec/rails/vendor/capybara.rb +35 -4
- data/lib/rspec/rails/version.rb +1 -1
- data/spec/autotest/rails_rspec2_spec.rb +5 -5
- data/spec/generators/rspec/controller/controller_generator_spec.rb +16 -16
- data/spec/generators/rspec/helper/helper_generator_spec.rb +4 -4
- data/spec/generators/rspec/install/install_generator_spec.rb +4 -4
- data/spec/generators/rspec/integration/integration_generator_spec.rb +18 -5
- data/spec/generators/rspec/mailer/mailer_generator_spec.rb +11 -11
- data/spec/generators/rspec/model/model_generator_spec.rb +7 -7
- data/spec/generators/rspec/observer/observer_generator_spec.rb +3 -3
- data/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +46 -34
- data/spec/generators/rspec/view/view_generator_spec.rb +6 -6
- data/spec/rspec/rails/assertion_adapter_spec.rb +3 -3
- data/spec/rspec/rails/configuration_spec.rb +2 -2
- data/spec/rspec/rails/deprecations_spec.rb +1 -1
- data/spec/rspec/rails/example/controller_example_group_spec.rb +47 -21
- data/spec/rspec/rails/example/feature_example_group_spec.rb +2 -2
- data/spec/rspec/rails/example/helper_example_group_spec.rb +12 -12
- data/spec/rspec/rails/example/mailer_example_group_spec.rb +3 -3
- data/spec/rspec/rails/example/model_example_group_spec.rb +3 -3
- data/spec/rspec/rails/example/request_example_group_spec.rb +5 -5
- data/spec/rspec/rails/example/routing_example_group_spec.rb +5 -5
- data/spec/rspec/rails/example/view_example_group_spec.rb +44 -29
- data/spec/rspec/rails/extensions/active_model/errors_on_spec.rb +3 -3
- data/spec/rspec/rails/fixture_support_spec.rb +3 -3
- data/spec/rspec/rails/matchers/be_a_new_spec.rb +17 -17
- data/spec/rspec/rails/matchers/be_new_record_spec.rb +2 -2
- data/spec/rspec/rails/matchers/be_routable_spec.rb +8 -8
- data/spec/rspec/rails/matchers/be_valid_spec.rb +3 -4
- data/spec/rspec/rails/matchers/errors_on_spec.rb +12 -12
- data/spec/rspec/rails/matchers/has_spec.rb +1 -1
- data/spec/rspec/rails/matchers/have_rendered_spec.rb +3 -3
- data/spec/rspec/rails/matchers/redirect_to_spec.rb +7 -6
- data/spec/rspec/rails/matchers/relation_match_array_spec.rb +5 -5
- data/spec/rspec/rails/matchers/route_to_spec.rb +21 -21
- data/spec/rspec/rails/minitest_lifecycle_adapter_spec.rb +9 -0
- data/spec/rspec/rails/mocks/mock_model_spec.rb +80 -57
- data/spec/rspec/rails/mocks/stub_model_spec.rb +23 -23
- data/spec/rspec/rails/setup_and_teardown_adapter_spec.rb +4 -4
- data/spec/rspec/rails/view_rendering_spec.rb +14 -14
- data/spec/spec_helper.rb +1 -0
- data/spec/support/helpers.rb +15 -1
- metadata +29 -13
@@ -7,7 +7,7 @@ describe "be_new_record" do
|
|
7
7
|
let(:record) { double('record', :persisted? => false) }
|
8
8
|
|
9
9
|
it "passes" do
|
10
|
-
record.
|
10
|
+
expect(record).to be_new_record
|
11
11
|
end
|
12
12
|
|
13
13
|
it "fails with custom failure message" do
|
@@ -21,7 +21,7 @@ describe "be_new_record" do
|
|
21
21
|
let(:record) { double('record', :persisted? => true) }
|
22
22
|
|
23
23
|
it "fails" do
|
24
|
-
record.
|
24
|
+
expect(record).not_to be_new_record
|
25
25
|
end
|
26
26
|
|
27
27
|
it "fails with custom failure message" do
|
@@ -8,16 +8,16 @@ describe "be_routable" do
|
|
8
8
|
|
9
9
|
context "with should" do
|
10
10
|
it "passes if routes recognize the path" do
|
11
|
-
routes.
|
11
|
+
allow(routes).to receive(:recognize_path) { {} }
|
12
12
|
expect do
|
13
|
-
{:get => "/a/path"}.
|
13
|
+
expect({:get => "/a/path"}).to be_routable
|
14
14
|
end.to_not raise_error
|
15
15
|
end
|
16
16
|
|
17
17
|
it "fails if routes do not recognize the path" do
|
18
|
-
routes.
|
18
|
+
allow(routes).to receive(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
|
19
19
|
expect do
|
20
|
-
{:get => "/a/path"}.
|
20
|
+
expect({:get => "/a/path"}).to be_routable
|
21
21
|
end.to raise_error(/expected \{:get=>"\/a\/path"\} to be routable/)
|
22
22
|
end
|
23
23
|
end
|
@@ -25,16 +25,16 @@ describe "be_routable" do
|
|
25
25
|
context "with should_not" do
|
26
26
|
|
27
27
|
it "passes if routes do not recognize the path" do
|
28
|
-
routes.
|
28
|
+
allow(routes).to receive(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
|
29
29
|
expect do
|
30
|
-
{:get => "/a/path"}.
|
30
|
+
expect({:get => "/a/path"}).not_to be_routable
|
31
31
|
end.to_not raise_error
|
32
32
|
end
|
33
33
|
|
34
34
|
it "fails if routes recognize the path" do
|
35
|
-
routes.
|
35
|
+
allow(routes).to receive(:recognize_path) { {:controller => "foo"} }
|
36
36
|
expect do
|
37
|
-
{:get => "/a/path"}.
|
37
|
+
expect({:get => "/a/path"}).not_to be_routable
|
38
38
|
end.to raise_error(/expected \{:get=>"\/a\/path"\} not to be routable, but it routes to \{:controller=>"foo"\}/)
|
39
39
|
end
|
40
40
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require "spec_helper"
|
2
|
-
require 'active_support/all'
|
3
2
|
require 'rspec/rails/matchers/be_valid'
|
4
3
|
|
5
4
|
describe "be_valid matcher" do
|
@@ -20,7 +19,7 @@ describe "be_valid matcher" do
|
|
20
19
|
end
|
21
20
|
|
22
21
|
it "includes a failure message for the negative case" do
|
23
|
-
post.
|
22
|
+
allow(post).to receive(:valid?) { true }
|
24
23
|
expect {
|
25
24
|
expect(post).not_to be_valid
|
26
25
|
}.to raise_exception(/expected .* not to be valid/)
|
@@ -33,12 +32,12 @@ describe "be_valid matcher" do
|
|
33
32
|
end
|
34
33
|
|
35
34
|
it "includes the validation context if provided" do
|
36
|
-
post.
|
35
|
+
expect(post).to receive(:valid?).with(:create) { true }
|
37
36
|
expect(post).to be_valid(:create)
|
38
37
|
end
|
39
38
|
|
40
39
|
it "does not include the validation context if not provided" do
|
41
|
-
post.
|
40
|
+
expect(post).to receive(:valid?).with(no_args) { true }
|
42
41
|
expect(post).to be_valid
|
43
42
|
end
|
44
43
|
end
|
@@ -2,37 +2,37 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe "error_on" do
|
4
4
|
it "provides a description including the name of what the error is on" do
|
5
|
-
have(1).error_on(:whatever).description.
|
5
|
+
expect(have(1).error_on(:whatever).description).to eq("have 1 error on :whatever")
|
6
6
|
end
|
7
7
|
|
8
8
|
it "provides a failure message including the number actually given" do
|
9
|
-
|
10
|
-
[].
|
11
|
-
}.
|
9
|
+
expect {
|
10
|
+
expect([]).to have(1).error_on(:whatever)
|
11
|
+
}.to raise_error("expected 1 error on :whatever, got 0")
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
describe "errors_on" do
|
16
16
|
it "provides a description including the name of what the error is on" do
|
17
|
-
have(2).errors_on(:whatever).description.
|
17
|
+
expect(have(2).errors_on(:whatever).description).to eq("have 2 errors on :whatever")
|
18
18
|
end
|
19
19
|
|
20
20
|
it "provides a failure message including the number actually given" do
|
21
|
-
|
22
|
-
[1].
|
23
|
-
}.
|
21
|
+
expect {
|
22
|
+
expect([1]).to have(3).errors_on(:whatever)
|
23
|
+
}.to raise_error("expected 3 errors on :whatever, got 1")
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
describe "have something other than error_on or errors_on" do
|
28
28
|
it "has a standard rspec failure message" do
|
29
|
-
|
30
|
-
[1,2,3].
|
31
|
-
}.
|
29
|
+
expect {
|
30
|
+
expect([1,2,3]).to have(2).elements
|
31
|
+
}.to raise_error("expected 2 elements, got 3")
|
32
32
|
end
|
33
33
|
|
34
34
|
it "has a standard rspec description" do
|
35
|
-
have(2).elements.description.
|
35
|
+
expect(have(2).elements.description).to eq("have 2 elements")
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -7,21 +7,21 @@ require "spec_helper"
|
|
7
7
|
|
8
8
|
context "given a hash" do
|
9
9
|
it "delegates to assert_template" do
|
10
|
-
self.
|
10
|
+
expect(self).to receive(:assert_template).with({:this => "hash"}, "this message")
|
11
11
|
expect("response").to send(template_expectation, {:this => "hash"}, "this message")
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
context "given a string" do
|
16
16
|
it "delegates to assert_template" do
|
17
|
-
self.
|
17
|
+
expect(self).to receive(:assert_template).with("this string", "this message")
|
18
18
|
expect("response").to send(template_expectation, "this string", "this message")
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
context "given a symbol" do
|
23
23
|
it "converts to_s and delegates to assert_template" do
|
24
|
-
self.
|
24
|
+
expect(self).to receive(:assert_template).with("template_name", "this message")
|
25
25
|
expect("response").to send(template_expectation, :template_name, "this message")
|
26
26
|
end
|
27
27
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require "spec_helper"
|
2
|
+
require "active_support"
|
2
3
|
require "active_support/test_case"
|
3
4
|
|
4
5
|
describe "redirect_to" do
|
@@ -12,7 +13,7 @@ describe "redirect_to" do
|
|
12
13
|
|
13
14
|
it "passes" do
|
14
15
|
expect do
|
15
|
-
response.
|
16
|
+
expect(response).to redirect_to("destination")
|
16
17
|
end.to_not raise_exception
|
17
18
|
end
|
18
19
|
end
|
@@ -24,7 +25,7 @@ describe "redirect_to" do
|
|
24
25
|
|
25
26
|
it "uses failure message from assert_redirected_to" do
|
26
27
|
expect do
|
27
|
-
response.
|
28
|
+
expect(response).to redirect_to("destination")
|
28
29
|
end.to raise_exception("this message")
|
29
30
|
end
|
30
31
|
end
|
@@ -36,7 +37,7 @@ describe "redirect_to" do
|
|
36
37
|
|
37
38
|
it "raises that exception" do
|
38
39
|
expect do
|
39
|
-
response.
|
40
|
+
expect(response).to redirect_to("destination")
|
40
41
|
end.to raise_exception("oops")
|
41
42
|
end
|
42
43
|
end
|
@@ -50,7 +51,7 @@ describe "redirect_to" do
|
|
50
51
|
|
51
52
|
it "passes" do
|
52
53
|
expect do
|
53
|
-
response.
|
54
|
+
expect(response).not_to redirect_to("destination")
|
54
55
|
end.to_not raise_exception
|
55
56
|
end
|
56
57
|
end
|
@@ -60,7 +61,7 @@ describe "redirect_to" do
|
|
60
61
|
|
61
62
|
it "fails with custom failure message" do
|
62
63
|
expect do
|
63
|
-
response.
|
64
|
+
expect(response).not_to redirect_to("destination")
|
64
65
|
end.to raise_exception(/expected not to redirect to \"destination\", but did/)
|
65
66
|
end
|
66
67
|
end
|
@@ -72,7 +73,7 @@ describe "redirect_to" do
|
|
72
73
|
|
73
74
|
it "raises that exception" do
|
74
75
|
expect do
|
75
|
-
response.
|
76
|
+
expect(response).not_to redirect_to("destination")
|
76
77
|
end.to raise_exception("oops")
|
77
78
|
end
|
78
79
|
end
|
@@ -7,25 +7,25 @@ describe "ActiveSupport::Relation =~ matcher" do
|
|
7
7
|
|
8
8
|
if ::Rails::VERSION::STRING >= '4'
|
9
9
|
it "verifies that the scope returns the records on the right hand side, regardless of order" do
|
10
|
-
MockableModel.all.
|
10
|
+
expect(MockableModel.all).to match_array(models.reverse)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "fails if the scope encompasses more records than on the right hand side" do
|
14
14
|
MockableModel.create
|
15
|
-
MockableModel.all.
|
15
|
+
expect(MockableModel.all).not_to match_array(models.reverse)
|
16
16
|
end
|
17
17
|
else
|
18
18
|
it "verifies that the scope returns the records on the right hand side, regardless of order" do
|
19
|
-
MockableModel.scoped.
|
19
|
+
expect(MockableModel.scoped).to match_array(models.reverse)
|
20
20
|
end
|
21
21
|
|
22
22
|
it "fails if the scope encompasses more records than on the right hand side" do
|
23
23
|
MockableModel.create
|
24
|
-
MockableModel.scoped.
|
24
|
+
expect(MockableModel.scoped).not_to match_array(models.reverse)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
it "fails if the scope encompasses fewer records than on the right hand side" do
|
29
|
-
MockableModel.limit(models.length - 1).
|
29
|
+
expect(MockableModel.limit(models.length - 1)).not_to match_array(models.reverse)
|
30
30
|
end
|
31
31
|
end
|
@@ -11,45 +11,45 @@ describe "route_to" do
|
|
11
11
|
it "provides a description" do
|
12
12
|
matcher = route_to("these" => "options")
|
13
13
|
matcher.matches?(:get => "path")
|
14
|
-
matcher.description.
|
14
|
+
expect(matcher.description).to eq("route {:get=>\"path\"} to {\"these\"=>\"options\"}")
|
15
15
|
end
|
16
16
|
|
17
17
|
it "delegates to assert_recognizes" do
|
18
|
-
self.
|
19
|
-
{:get => "path"}.
|
18
|
+
expect(self).to receive(:assert_recognizes).with({ "these" => "options" }, { :method=> :get, :path=>"path" }, {})
|
19
|
+
expect({:get => "path"}).to route_to("these" => "options")
|
20
20
|
end
|
21
21
|
|
22
22
|
context "with shortcut syntax" do
|
23
23
|
it "routes with extra options" do
|
24
|
-
self.
|
25
|
-
get("path").
|
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
26
|
end
|
27
27
|
|
28
28
|
it "routes without extra options" do
|
29
|
-
self.
|
29
|
+
expect(self).to receive(:assert_recognizes).with(
|
30
30
|
{:controller => "controller", :action => "action"},
|
31
31
|
{:method=> :get, :path=>"path" },
|
32
32
|
{}
|
33
33
|
)
|
34
|
-
get("path").
|
34
|
+
expect(get("path")).to route_to("controller#action")
|
35
35
|
end
|
36
36
|
|
37
37
|
it "routes with one query parameter" do
|
38
|
-
self.
|
38
|
+
expect(self).to receive(:assert_recognizes).with(
|
39
39
|
{:controller => "controller", :action => "action", :queryitem => "queryvalue"},
|
40
40
|
{:method=> :get, :path=>"path" },
|
41
41
|
{'queryitem' => 'queryvalue' }
|
42
42
|
)
|
43
|
-
get("path?queryitem=queryvalue").
|
43
|
+
expect(get("path?queryitem=queryvalue")).to route_to("controller#action", :queryitem => 'queryvalue')
|
44
44
|
end
|
45
45
|
|
46
46
|
it "routes with multiple query parameters" do
|
47
|
-
self.
|
47
|
+
expect(self).to receive(:assert_recognizes).with(
|
48
48
|
{:controller => "controller", :action => "action", :queryitem => "queryvalue", :qi2 => 'qv2'},
|
49
49
|
{:method=> :get, :path=>"path"},
|
50
50
|
{'queryitem' => 'queryvalue', 'qi2' => 'qv2'}
|
51
51
|
)
|
52
|
-
get("path?queryitem=queryvalue&qi2=qv2").
|
52
|
+
expect(get("path?queryitem=queryvalue&qi2=qv2")).to route_to("controller#action", :queryitem => 'queryvalue', :qi2 => 'qv2')
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|
@@ -58,7 +58,7 @@ describe "route_to" do
|
|
58
58
|
context "when assert_recognizes passes" do
|
59
59
|
it "passes" do
|
60
60
|
expect do
|
61
|
-
{:get => "path"}.
|
61
|
+
expect({:get => "path"}).to route_to("these" => "options")
|
62
62
|
end.to_not raise_exception
|
63
63
|
end
|
64
64
|
end
|
@@ -69,7 +69,7 @@ describe "route_to" do
|
|
69
69
|
raise ActiveSupport::TestCase::Assertion.new("this message")
|
70
70
|
end
|
71
71
|
expect do
|
72
|
-
{:get => "path"}.
|
72
|
+
expect({:get => "path"}).to route_to("these" => "options")
|
73
73
|
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "this message")
|
74
74
|
end
|
75
75
|
end
|
@@ -80,7 +80,7 @@ describe "route_to" do
|
|
80
80
|
raise ActionController::RoutingError.new("this message")
|
81
81
|
end
|
82
82
|
expect do
|
83
|
-
{:get => "path"}.
|
83
|
+
expect({:get => "path"}).to route_to("these" => "options")
|
84
84
|
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "this message")
|
85
85
|
end
|
86
86
|
end
|
@@ -91,7 +91,7 @@ describe "route_to" do
|
|
91
91
|
raise "oops"
|
92
92
|
end
|
93
93
|
expect do
|
94
|
-
{:get => "path"}.
|
94
|
+
expect({:get => "path"}).to route_to("these" => "options")
|
95
95
|
end.to raise_exception("oops")
|
96
96
|
end
|
97
97
|
end
|
@@ -101,8 +101,8 @@ describe "route_to" do
|
|
101
101
|
context "when assert_recognizes passes" do
|
102
102
|
it "fails with custom message" do
|
103
103
|
expect do
|
104
|
-
{:get => "path"}.
|
105
|
-
end.to raise_error(/expected
|
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
106
|
end
|
107
107
|
end
|
108
108
|
|
@@ -112,7 +112,7 @@ describe "route_to" do
|
|
112
112
|
raise ActiveSupport::TestCase::Assertion.new("this message")
|
113
113
|
end
|
114
114
|
expect do
|
115
|
-
{:get => "path"}.
|
115
|
+
expect({:get => "path"}).not_to route_to("these" => "options")
|
116
116
|
end.to_not raise_error
|
117
117
|
end
|
118
118
|
end
|
@@ -123,7 +123,7 @@ describe "route_to" do
|
|
123
123
|
raise ActionController::RoutingError.new("this message")
|
124
124
|
end
|
125
125
|
expect do
|
126
|
-
{:get => "path"}.
|
126
|
+
expect({:get => "path"}).not_to route_to("these" => "options")
|
127
127
|
end.to_not raise_error
|
128
128
|
end
|
129
129
|
end
|
@@ -134,7 +134,7 @@ describe "route_to" do
|
|
134
134
|
raise "oops"
|
135
135
|
end
|
136
136
|
expect do
|
137
|
-
{:get => "path"}.
|
137
|
+
expect({:get => "path"}).not_to route_to("these" => "options")
|
138
138
|
end.to raise_exception("oops")
|
139
139
|
end
|
140
140
|
end
|
@@ -145,7 +145,7 @@ describe "route_to" do
|
|
145
145
|
raise ActiveSupport::TestCase::Assertion, "this message"
|
146
146
|
end
|
147
147
|
expect do
|
148
|
-
{"this" => "path"}.
|
148
|
+
expect({"this" => "path"}).to route_to("these" => "options")
|
149
149
|
end.to raise_error("this message")
|
150
150
|
end
|
151
151
|
end
|
@@ -19,4 +19,13 @@ describe RSpec::Rails::MinitestLifecycleAdapter do
|
|
19
19
|
:before_setup, :after_setup, :example, :before_teardown, :after_teardown
|
20
20
|
])
|
21
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
|
22
31
|
end
|
@@ -6,7 +6,7 @@ describe "mock_model(RealModel)" do
|
|
6
6
|
context "that does not represent an existing constant" do
|
7
7
|
it "class says it's name" do
|
8
8
|
model = mock_model("Foo")
|
9
|
-
model.class.name.
|
9
|
+
expect(model.class.name).to eq("Foo")
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
@@ -14,7 +14,7 @@ describe "mock_model(RealModel)" do
|
|
14
14
|
context "that extends ActiveModel::Naming" do
|
15
15
|
it "treats the constant as the class" do
|
16
16
|
model = mock_model("MockableModel")
|
17
|
-
model.class.name.
|
17
|
+
expect(model.class.name).to eq("MockableModel")
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -42,7 +42,7 @@ describe "mock_model(RealModel)" do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
it "is named using the stubbed id value" do
|
45
|
-
@model.instance_variable_get(:@name).
|
45
|
+
expect(@model.instance_variable_get(:@name)).to eq("MockableModel_1")
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -50,7 +50,30 @@ describe "mock_model(RealModel)" do
|
|
50
50
|
it "sets persisted to false" do
|
51
51
|
model = mock_model(MockableModel)
|
52
52
|
model.destroy
|
53
|
-
model.
|
53
|
+
expect(model).not_to be_persisted
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "association" do
|
58
|
+
it "constructs a mock association object" do
|
59
|
+
model = mock_model(MockableModel)
|
60
|
+
expect(model.association(:association_name)).to be
|
61
|
+
end
|
62
|
+
|
63
|
+
it "returns a different association object for each association name" do
|
64
|
+
model = mock_model(MockableModel)
|
65
|
+
posts = model.association(:posts)
|
66
|
+
authors = model.association(:authors)
|
67
|
+
|
68
|
+
expect(posts).not_to equal(authors)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns the same association model each time for the same association name" do
|
72
|
+
model = mock_model(MockableModel)
|
73
|
+
posts1 = model.association(:posts)
|
74
|
+
posts2 = model.association(:posts)
|
75
|
+
|
76
|
+
expect(posts1).to equal(posts2)
|
54
77
|
end
|
55
78
|
end
|
56
79
|
|
@@ -58,21 +81,21 @@ describe "mock_model(RealModel)" do
|
|
58
81
|
context "default" do
|
59
82
|
it "is empty" do
|
60
83
|
model = mock_model(MockableModel)
|
61
|
-
model.errors.
|
84
|
+
expect(model.errors).to be_empty
|
62
85
|
end
|
63
86
|
end
|
64
87
|
|
65
88
|
context "with :save => false" do
|
66
89
|
it "is not empty" do
|
67
90
|
model = mock_model(MockableModel, :save => false)
|
68
|
-
model.errors.
|
91
|
+
expect(model.errors).not_to be_empty
|
69
92
|
end
|
70
93
|
end
|
71
94
|
|
72
95
|
context "with :update_attributes => false" do
|
73
96
|
it "is not empty" do
|
74
97
|
model = mock_model(MockableModel, :save => false)
|
75
|
-
model.errors.
|
98
|
+
expect(model.errors).not_to be_empty
|
76
99
|
end
|
77
100
|
end
|
78
101
|
end
|
@@ -81,7 +104,7 @@ describe "mock_model(RealModel)" do
|
|
81
104
|
it "does not mutate its parameters" do
|
82
105
|
params = {:a => 'b'}
|
83
106
|
mock_model(MockableModel, params)
|
84
|
-
params.
|
107
|
+
expect(params).to eq({:a => 'b'})
|
85
108
|
end
|
86
109
|
end
|
87
110
|
|
@@ -93,11 +116,11 @@ describe "mock_model(RealModel)" do
|
|
93
116
|
end
|
94
117
|
|
95
118
|
it "passes: associated_model == mock" do
|
96
|
-
@mock_model.
|
119
|
+
expect(@mock_model).to eq(@real.mockable_model)
|
97
120
|
end
|
98
121
|
|
99
122
|
it "passes: mock == associated_model" do
|
100
|
-
@real.mockable_model.
|
123
|
+
expect(@real.mockable_model).to eq(@mock_model)
|
101
124
|
end
|
102
125
|
end
|
103
126
|
|
@@ -109,11 +132,11 @@ describe "mock_model(RealModel)" do
|
|
109
132
|
end
|
110
133
|
|
111
134
|
it "passes: associated_model == mock" do
|
112
|
-
@mock_model.
|
135
|
+
expect(@mock_model).to eq(@real.nonexistent_model)
|
113
136
|
end
|
114
137
|
|
115
138
|
it "passes: mock == associated_model" do
|
116
|
-
@real.nonexistent_model.
|
139
|
+
expect(@real.nonexistent_model).to eq(@mock_model)
|
117
140
|
end
|
118
141
|
end
|
119
142
|
|
@@ -123,15 +146,15 @@ describe "mock_model(RealModel)" do
|
|
123
146
|
end
|
124
147
|
|
125
148
|
it "says it is_a?(RealModel)" do
|
126
|
-
@model.is_a?(SubMockableModel).
|
149
|
+
expect(@model.is_a?(SubMockableModel)).to be(true)
|
127
150
|
end
|
128
151
|
|
129
152
|
it "says it is_a?(OtherModel) if RealModel is an ancestors" do
|
130
|
-
@model.is_a?(MockableModel).
|
153
|
+
expect(@model.is_a?(MockableModel)).to be(true)
|
131
154
|
end
|
132
155
|
|
133
156
|
it "can be stubbed" do
|
134
|
-
mock_model(MockableModel, :is_a? => true).is_a?(:Foo).
|
157
|
+
expect(mock_model(MockableModel, :is_a? => true).is_a?(:Foo)).to be_truthy
|
135
158
|
end
|
136
159
|
end
|
137
160
|
|
@@ -141,15 +164,15 @@ describe "mock_model(RealModel)" do
|
|
141
164
|
end
|
142
165
|
|
143
166
|
it "says it is kind_of? if RealModel is" do
|
144
|
-
@model.kind_of?(SubMockableModel).
|
167
|
+
expect(@model.kind_of?(SubMockableModel)).to be(true)
|
145
168
|
end
|
146
169
|
|
147
170
|
it "says it is kind_of? if RealModel's ancestor is" do
|
148
|
-
@model.kind_of?(MockableModel).
|
171
|
+
expect(@model.kind_of?(MockableModel)).to be(true)
|
149
172
|
end
|
150
173
|
|
151
174
|
it "can be stubbed" do
|
152
|
-
mock_model(MockableModel, :kind_of? => true).kind_of?(:Foo).
|
175
|
+
expect(mock_model(MockableModel, :kind_of? => true).kind_of?(:Foo)).to be_truthy
|
153
176
|
end
|
154
177
|
end
|
155
178
|
|
@@ -159,22 +182,22 @@ describe "mock_model(RealModel)" do
|
|
159
182
|
end
|
160
183
|
|
161
184
|
it "says it is instance_of? if RealModel is" do
|
162
|
-
@model.instance_of?(SubMockableModel).
|
185
|
+
expect(@model.instance_of?(SubMockableModel)).to be(true)
|
163
186
|
end
|
164
187
|
|
165
188
|
it "does not say it instance_of? if RealModel isn't, even if it's ancestor is" do
|
166
|
-
@model.instance_of?(MockableModel).
|
189
|
+
expect(@model.instance_of?(MockableModel)).to be(false)
|
167
190
|
end
|
168
191
|
|
169
192
|
it "can be stubbed" do
|
170
|
-
mock_model(MockableModel, :instance_of? => true).instance_of?(:Foo).
|
193
|
+
expect(mock_model(MockableModel, :instance_of? => true).instance_of?(:Foo)).to be_truthy
|
171
194
|
end
|
172
195
|
end
|
173
196
|
|
174
197
|
describe "#respond_to?" do
|
175
198
|
context "with an ActiveRecord model" do
|
176
199
|
before(:each) do
|
177
|
-
MockableModel.
|
200
|
+
allow(MockableModel).to receive(:column_names).and_return(["column_a", "column_b"])
|
178
201
|
@model = mock_model(MockableModel)
|
179
202
|
end
|
180
203
|
|
@@ -186,57 +209,57 @@ describe "mock_model(RealModel)" do
|
|
186
209
|
|
187
210
|
context "without as_null_object" do
|
188
211
|
it "says it will respond_to?(key) if RealModel has the attribute 'key'" do
|
189
|
-
@model.respond_to?("column_a").
|
212
|
+
expect(@model.respond_to?("column_a")).to be(true)
|
190
213
|
end
|
191
214
|
it "stubs column accessor (with string)" do
|
192
215
|
@model.respond_to?("column_a")
|
193
|
-
@model.column_a.
|
216
|
+
expect(@model.column_a).to be_nil
|
194
217
|
end
|
195
218
|
it "stubs column accessor (with symbol)" do
|
196
219
|
@model.respond_to?(:column_a)
|
197
|
-
@model.column_a.
|
220
|
+
expect(@model.column_a).to be_nil
|
198
221
|
end
|
199
222
|
it "does not stub column accessor if already stubbed in declaration (with string)" do
|
200
223
|
model = mock_model(MockableModel, "column_a" => "a")
|
201
224
|
model.respond_to?("column_a")
|
202
|
-
model.column_a.
|
225
|
+
expect(model.column_a).to eq("a")
|
203
226
|
end
|
204
227
|
it "does not stub column accessor if already stubbed in declaration (with symbol)" do
|
205
228
|
model = mock_model(MockableModel, :column_a => "a")
|
206
229
|
model.respond_to?("column_a")
|
207
|
-
model.column_a.
|
230
|
+
expect(model.column_a).to eq("a")
|
208
231
|
end
|
209
232
|
it "does not stub column accessor if already stubbed after declaration (with string)" do
|
210
|
-
@model.
|
233
|
+
allow(@model).to receive(:column_a).and_return("a")
|
211
234
|
@model.respond_to?("column_a")
|
212
|
-
@model.column_a.
|
235
|
+
expect(@model.column_a).to eq("a")
|
213
236
|
end
|
214
237
|
it "does not stub column accessor if already stubbed after declaration (with symbol)" do
|
215
|
-
@model.
|
238
|
+
allow(@model).to receive(:column_a).and_return("a")
|
216
239
|
@model.respond_to?("column_a")
|
217
|
-
@model.column_a.
|
240
|
+
expect(@model.column_a).to eq("a")
|
218
241
|
end
|
219
242
|
it "says it will not respond_to?(key) if RealModel does not have the attribute 'key'" do
|
220
|
-
@model.respond_to?("column_c").
|
243
|
+
expect(@model.respond_to?("column_c")).to be(false)
|
221
244
|
end
|
222
245
|
it "says it will not respond_to?(xxx_before_type_cast)" do
|
223
|
-
@model.respond_to?("title_before_type_cast").
|
246
|
+
expect(@model.respond_to?("title_before_type_cast")).to be(false)
|
224
247
|
end
|
225
248
|
end
|
226
|
-
|
249
|
+
|
227
250
|
context "with as_null_object" do
|
228
251
|
it "says it will respond_to?(key) if RealModel has the attribute 'key'" do
|
229
|
-
@model.as_null_object.respond_to?("column_a").
|
252
|
+
expect(@model.as_null_object.respond_to?("column_a")).to be(true)
|
230
253
|
end
|
231
254
|
it "says it will respond_to?(key) even if RealModel does not have the attribute 'key'" do
|
232
|
-
@model.as_null_object.respond_to?("column_c").
|
255
|
+
expect(@model.as_null_object.respond_to?("column_c")).to be(true)
|
233
256
|
end
|
234
257
|
it "says it will not respond_to?(xxx_before_type_cast)" do
|
235
|
-
@model.as_null_object.respond_to?("title_before_type_cast").
|
258
|
+
expect(@model.as_null_object.respond_to?("title_before_type_cast")).to be(false)
|
236
259
|
end
|
237
260
|
it "returns self for any unprepared message" do
|
238
261
|
@model.as_null_object.tap do |x|
|
239
|
-
x.non_existant_message.
|
262
|
+
expect(x.non_existant_message).to be(@model)
|
240
263
|
end
|
241
264
|
end
|
242
265
|
end
|
@@ -245,39 +268,39 @@ describe "mock_model(RealModel)" do
|
|
245
268
|
context "with a non-ActiveRecord model" do
|
246
269
|
it "responds as normal" do
|
247
270
|
model = NonActiveRecordModel.new
|
248
|
-
model.
|
271
|
+
expect(model).to respond_to(:to_param)
|
249
272
|
end
|
250
273
|
|
251
274
|
context "with as_null_object" do
|
252
275
|
it "says it will not respond_to?(xxx_before_type_cast)" do
|
253
276
|
model = NonActiveRecordModel.new.as_null_object
|
254
|
-
model.respond_to?("title_before_type_cast").
|
277
|
+
expect(model.respond_to?("title_before_type_cast")).to be(false)
|
255
278
|
end
|
256
279
|
end
|
257
280
|
end
|
258
281
|
|
259
282
|
it "can be stubbed" do
|
260
|
-
mock_model(MockableModel, :respond_to? => true).respond_to?(:foo).
|
283
|
+
expect(mock_model(MockableModel, :respond_to? => true).respond_to?(:foo)).to be_truthy
|
261
284
|
end
|
262
285
|
end
|
263
286
|
|
264
287
|
describe "#class" do
|
265
288
|
it "returns the mocked model" do
|
266
|
-
mock_model(MockableModel).class.
|
289
|
+
expect(mock_model(MockableModel).class).to eq(MockableModel)
|
267
290
|
end
|
268
291
|
|
269
292
|
it "can be stubbed" do
|
270
|
-
mock_model(MockableModel, :class => String).class.
|
293
|
+
expect(mock_model(MockableModel, :class => String).class).to be(String)
|
271
294
|
end
|
272
295
|
end
|
273
296
|
|
274
297
|
describe "#to_s" do
|
275
298
|
it "returns (model.name)_(model#to_param)" do
|
276
|
-
mock_model(MockableModel).to_s.
|
299
|
+
expect(mock_model(MockableModel).to_s).to eq("MockableModel_#{to_param}")
|
277
300
|
end
|
278
301
|
|
279
302
|
it "can be stubbed" do
|
280
|
-
mock_model(MockableModel, :to_s => "this string").to_s.
|
303
|
+
expect(mock_model(MockableModel, :to_s => "this string").to_s).to eq("this string")
|
281
304
|
end
|
282
305
|
end
|
283
306
|
|
@@ -285,7 +308,7 @@ describe "mock_model(RealModel)" do
|
|
285
308
|
context "default" do
|
286
309
|
it "returns false" do
|
287
310
|
@model = mock_model(SubMockableModel)
|
288
|
-
@model.destroyed
|
311
|
+
expect(@model.destroyed?).to be(false)
|
289
312
|
end
|
290
313
|
end
|
291
314
|
end
|
@@ -294,7 +317,7 @@ describe "mock_model(RealModel)" do
|
|
294
317
|
context "default" do
|
295
318
|
it "returns false" do
|
296
319
|
@model = mock_model(SubMockableModel)
|
297
|
-
@model.marked_for_destruction
|
320
|
+
expect(@model.marked_for_destruction?).to be(false)
|
298
321
|
end
|
299
322
|
end
|
300
323
|
end
|
@@ -302,19 +325,19 @@ describe "mock_model(RealModel)" do
|
|
302
325
|
describe "#persisted?" do
|
303
326
|
context "with default identifier" do
|
304
327
|
it "returns true" do
|
305
|
-
mock_model(MockableModel).
|
328
|
+
expect(mock_model(MockableModel)).to be_persisted
|
306
329
|
end
|
307
330
|
end
|
308
331
|
|
309
332
|
context "with explicit identifier via :id" do
|
310
333
|
it "returns true" do
|
311
|
-
mock_model(MockableModel, :id => 37).
|
334
|
+
expect(mock_model(MockableModel, :id => 37)).to be_persisted
|
312
335
|
end
|
313
336
|
end
|
314
337
|
|
315
338
|
context "with id => nil" do
|
316
339
|
it "returns false" do
|
317
|
-
mock_model(MockableModel, :id => nil).
|
340
|
+
expect(mock_model(MockableModel, :id => nil)).not_to be_persisted
|
318
341
|
end
|
319
342
|
end
|
320
343
|
end
|
@@ -322,13 +345,13 @@ describe "mock_model(RealModel)" do
|
|
322
345
|
describe "#valid?" do
|
323
346
|
context "default" do
|
324
347
|
it "returns true" do
|
325
|
-
mock_model(MockableModel).
|
348
|
+
expect(mock_model(MockableModel)).to be_valid
|
326
349
|
end
|
327
350
|
end
|
328
351
|
|
329
352
|
context "stubbed with false" do
|
330
353
|
it "returns false" do
|
331
|
-
mock_model(MockableModel, :valid? => false).
|
354
|
+
expect(mock_model(MockableModel, :valid? => false)).not_to be_valid
|
332
355
|
end
|
333
356
|
end
|
334
357
|
end
|
@@ -336,26 +359,26 @@ describe "mock_model(RealModel)" do
|
|
336
359
|
describe "#as_new_record" do
|
337
360
|
it "says it is a new record" do
|
338
361
|
m = mock_model(MockableModel)
|
339
|
-
m.as_new_record.
|
362
|
+
expect(m.as_new_record).to be_new_record
|
340
363
|
end
|
341
364
|
|
342
365
|
it "says it is not persisted" do
|
343
366
|
m = mock_model(MockableModel)
|
344
|
-
m.as_new_record.
|
367
|
+
expect(m.as_new_record).not_to be_persisted
|
345
368
|
end
|
346
369
|
|
347
370
|
it "has a nil id" do
|
348
|
-
mock_model(MockableModel).as_new_record.id.
|
371
|
+
expect(mock_model(MockableModel).as_new_record.id).to be(nil)
|
349
372
|
end
|
350
373
|
|
351
374
|
it "returns nil for #to_param" do
|
352
|
-
mock_model(MockableModel).as_new_record.to_param.
|
375
|
+
expect(mock_model(MockableModel).as_new_record.to_param).to be(nil)
|
353
376
|
end
|
354
377
|
end
|
355
378
|
|
356
379
|
describe "#blank?" do
|
357
380
|
it "is false" do
|
358
|
-
mock_model(MockableModel).
|
381
|
+
expect(mock_model(MockableModel)).not_to be_blank
|
359
382
|
end
|
360
383
|
end
|
361
384
|
|