rspec-expectations 2.14.0 → 3.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/.document +1 -1
- data/.yardopts +1 -1
- data/Changelog.md +976 -25
- data/{License.txt → LICENSE.md} +5 -3
- data/README.md +162 -26
- data/lib/rspec/expectations/block_snippet_extractor.rb +253 -0
- data/lib/rspec/expectations/configuration.rb +230 -0
- data/lib/rspec/expectations/expectation_target.rb +127 -51
- data/lib/rspec/expectations/fail_with.rb +17 -57
- data/lib/rspec/expectations/failure_aggregator.rb +229 -0
- data/lib/rspec/expectations/handler.rb +146 -32
- data/lib/rspec/expectations/minitest_integration.rb +58 -0
- data/lib/rspec/expectations/syntax.rb +68 -100
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/expectations.rb +58 -23
- data/lib/rspec/matchers/aliased_matcher.rb +116 -0
- data/lib/rspec/matchers/built_in/all.rb +86 -0
- data/lib/rspec/matchers/built_in/base_matcher.rb +191 -20
- data/lib/rspec/matchers/built_in/be.rb +114 -114
- data/lib/rspec/matchers/built_in/be_between.rb +77 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +15 -4
- data/lib/rspec/matchers/built_in/be_kind_of.rb +10 -1
- data/lib/rspec/matchers/built_in/be_within.rb +35 -18
- data/lib/rspec/matchers/built_in/change.rb +389 -80
- data/lib/rspec/matchers/built_in/compound.rb +290 -0
- data/lib/rspec/matchers/built_in/contain_exactly.rb +310 -0
- data/lib/rspec/matchers/built_in/count_expectation.rb +169 -0
- data/lib/rspec/matchers/built_in/cover.rb +3 -0
- data/lib/rspec/matchers/built_in/eq.rb +30 -8
- data/lib/rspec/matchers/built_in/eql.rb +23 -8
- data/lib/rspec/matchers/built_in/equal.rb +55 -22
- data/lib/rspec/matchers/built_in/exist.rb +74 -10
- data/lib/rspec/matchers/built_in/has.rb +141 -22
- data/lib/rspec/matchers/built_in/have_attributes.rb +114 -0
- data/lib/rspec/matchers/built_in/include.rb +184 -32
- data/lib/rspec/matchers/built_in/match.rb +95 -1
- data/lib/rspec/matchers/built_in/operators.rb +128 -0
- data/lib/rspec/matchers/built_in/output.rb +207 -0
- data/lib/rspec/matchers/built_in/raise_error.rb +192 -44
- data/lib/rspec/matchers/built_in/respond_to.rb +154 -28
- data/lib/rspec/matchers/built_in/satisfy.rb +39 -9
- data/lib/rspec/matchers/built_in/start_or_end_with.rb +94 -0
- data/lib/rspec/matchers/built_in/throw_symbol.rb +58 -14
- data/lib/rspec/matchers/built_in/yield.rb +240 -161
- data/lib/rspec/matchers/built_in.rb +47 -33
- data/lib/rspec/matchers/composable.rb +171 -0
- data/lib/rspec/matchers/dsl.rb +531 -10
- data/lib/rspec/matchers/english_phrasing.rb +58 -0
- data/lib/rspec/matchers/fail_matchers.rb +42 -0
- data/lib/rspec/matchers/generated_descriptions.rb +14 -8
- data/lib/rspec/matchers/matcher_delegator.rb +61 -0
- data/lib/rspec/matchers/matcher_protocol.rb +105 -0
- data/lib/rspec/matchers/multi_matcher_diff.rb +82 -0
- data/lib/rspec/matchers.rb +520 -173
- data.tar.gz.sig +0 -0
- metadata +141 -242
- metadata.gz.sig +2 -0
- data/features/README.md +0 -48
- data/features/Upgrade.md +0 -53
- data/features/built_in_matchers/README.md +0 -90
- data/features/built_in_matchers/be.feature +0 -175
- data/features/built_in_matchers/be_within.feature +0 -48
- data/features/built_in_matchers/cover.feature +0 -47
- data/features/built_in_matchers/end_with.feature +0 -48
- data/features/built_in_matchers/equality.feature +0 -139
- data/features/built_in_matchers/exist.feature +0 -45
- data/features/built_in_matchers/expect_change.feature +0 -59
- data/features/built_in_matchers/expect_error.feature +0 -144
- data/features/built_in_matchers/have.feature +0 -109
- data/features/built_in_matchers/include.feature +0 -174
- data/features/built_in_matchers/match.feature +0 -52
- data/features/built_in_matchers/operators.feature +0 -227
- data/features/built_in_matchers/predicates.feature +0 -137
- data/features/built_in_matchers/respond_to.feature +0 -84
- data/features/built_in_matchers/satisfy.feature +0 -33
- data/features/built_in_matchers/start_with.feature +0 -48
- data/features/built_in_matchers/throw_symbol.feature +0 -91
- data/features/built_in_matchers/types.feature +0 -116
- data/features/built_in_matchers/yield.feature +0 -161
- data/features/custom_matchers/access_running_example.feature +0 -53
- data/features/custom_matchers/define_diffable_matcher.feature +0 -27
- data/features/custom_matchers/define_matcher.feature +0 -368
- data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -38
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
- data/features/customized_message.feature +0 -22
- data/features/diffing.feature +0 -85
- data/features/implicit_docstrings.feature +0 -52
- data/features/step_definitions/additional_cli_steps.rb +0 -22
- data/features/support/env.rb +0 -14
- data/features/syntax_configuration.feature +0 -71
- data/features/test_frameworks/test_unit.feature +0 -44
- data/lib/rspec/expectations/deprecation.rb +0 -17
- data/lib/rspec/expectations/differ.rb +0 -133
- data/lib/rspec/expectations/errors.rb +0 -9
- data/lib/rspec/expectations/extensions/array.rb +0 -9
- data/lib/rspec/expectations/extensions/object.rb +0 -29
- data/lib/rspec/expectations/extensions.rb +0 -2
- data/lib/rspec/matchers/be_close.rb +0 -9
- data/lib/rspec/matchers/built_in/have.rb +0 -124
- data/lib/rspec/matchers/built_in/match_array.rb +0 -51
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +0 -48
- data/lib/rspec/matchers/compatibility.rb +0 -14
- data/lib/rspec/matchers/configuration.rb +0 -108
- data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
- data/lib/rspec/matchers/matcher.rb +0 -300
- data/lib/rspec/matchers/method_missing.rb +0 -12
- data/lib/rspec/matchers/operator_matcher.rb +0 -109
- data/lib/rspec/matchers/pretty.rb +0 -70
- data/lib/rspec/matchers/test_unit_integration.rb +0 -11
- data/lib/rspec-expectations.rb +0 -1
- data/spec/rspec/expectations/differ_spec.rb +0 -192
- data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
- data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
- data/spec/rspec/expectations/fail_with_spec.rb +0 -114
- data/spec/rspec/expectations/handler_spec.rb +0 -227
- data/spec/rspec/expectations/syntax_spec.rb +0 -139
- data/spec/rspec/matchers/base_matcher_spec.rb +0 -62
- data/spec/rspec/matchers/be_close_spec.rb +0 -22
- data/spec/rspec/matchers/be_instance_of_spec.rb +0 -63
- data/spec/rspec/matchers/be_kind_of_spec.rb +0 -41
- data/spec/rspec/matchers/be_spec.rb +0 -516
- data/spec/rspec/matchers/be_within_spec.rb +0 -137
- data/spec/rspec/matchers/change_spec.rb +0 -553
- data/spec/rspec/matchers/configuration_spec.rb +0 -206
- data/spec/rspec/matchers/cover_spec.rb +0 -69
- data/spec/rspec/matchers/description_generation_spec.rb +0 -190
- data/spec/rspec/matchers/dsl_spec.rb +0 -57
- data/spec/rspec/matchers/eq_spec.rb +0 -60
- data/spec/rspec/matchers/eql_spec.rb +0 -41
- data/spec/rspec/matchers/equal_spec.rb +0 -78
- data/spec/rspec/matchers/exist_spec.rb +0 -124
- data/spec/rspec/matchers/has_spec.rb +0 -122
- data/spec/rspec/matchers/have_spec.rb +0 -455
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
- data/spec/rspec/matchers/include_spec.rb +0 -531
- data/spec/rspec/matchers/match_array_spec.rb +0 -194
- data/spec/rspec/matchers/match_spec.rb +0 -61
- data/spec/rspec/matchers/matcher_spec.rb +0 -471
- data/spec/rspec/matchers/matchers_spec.rb +0 -37
- data/spec/rspec/matchers/method_missing_spec.rb +0 -28
- data/spec/rspec/matchers/operator_matcher_spec.rb +0 -223
- data/spec/rspec/matchers/raise_error_spec.rb +0 -485
- data/spec/rspec/matchers/respond_to_spec.rb +0 -292
- data/spec/rspec/matchers/satisfy_spec.rb +0 -44
- data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -186
- data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
- data/spec/rspec/matchers/yield_spec.rb +0 -514
- data/spec/spec_helper.rb +0 -54
- data/spec/support/classes.rb +0 -56
- data/spec/support/in_sub_process.rb +0 -38
- data/spec/support/matchers.rb +0 -22
- data/spec/support/ruby_version.rb +0 -10
- data/spec/support/shared_examples.rb +0 -13
@@ -1,137 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Matchers
|
5
|
-
describe "expect(actual).to be_within(delta).of(expected)" do
|
6
|
-
it_behaves_like "an RSpec matcher", :valid_value => 5, :invalid_value => -5 do
|
7
|
-
let(:matcher) { be_within(2).of(4.0) }
|
8
|
-
end
|
9
|
-
|
10
|
-
it "passes when actual == expected" do
|
11
|
-
expect(5.0).to be_within(0.5).of(5.0)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "passes when actual < (expected + delta)" do
|
15
|
-
expect(5.49).to be_within(0.5).of(5.0)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "passes when actual > (expected - delta)" do
|
19
|
-
expect(4.51).to be_within(0.5).of(5.0)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "passes when actual == (expected - delta)" do
|
23
|
-
expect(4.5).to be_within(0.5).of(5.0)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "passes when actual == (expected + delta)" do
|
27
|
-
expect(5.5).to be_within(0.5).of(5.0)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "passes with integer arguments that are near each other" do
|
31
|
-
expect(1.0001).to be_within(5).percent_of(1)
|
32
|
-
end
|
33
|
-
|
34
|
-
it "passes with negative arguments" do
|
35
|
-
expect(-1.0001).to be_within(5).percent_of(-1)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "fails when actual < (expected - delta)" do
|
39
|
-
expect {
|
40
|
-
expect(4.49).to be_within(0.5).of(5.0)
|
41
|
-
}.to fail_with("expected 4.49 to be within 0.5 of 5.0")
|
42
|
-
end
|
43
|
-
|
44
|
-
it "fails when actual > (expected + delta)" do
|
45
|
-
expect {
|
46
|
-
expect(5.51).to be_within(0.5).of(5.0)
|
47
|
-
}.to fail_with("expected 5.51 to be within 0.5 of 5.0")
|
48
|
-
end
|
49
|
-
|
50
|
-
it "works with Time" do
|
51
|
-
expect(Time.now).to be_within(0.1).of(Time.now)
|
52
|
-
end
|
53
|
-
|
54
|
-
it "provides a description" do
|
55
|
-
matcher = be_within(0.5).of(5.0)
|
56
|
-
matcher.matches?(5.1)
|
57
|
-
expect(matcher.description).to eq "be within 0.5 of 5.0"
|
58
|
-
end
|
59
|
-
|
60
|
-
it "raises an error if no expected value is given" do
|
61
|
-
expect {
|
62
|
-
expect(5.1).to be_within(0.5)
|
63
|
-
}.to raise_error(ArgumentError, /must set an expected value using #of/)
|
64
|
-
end
|
65
|
-
|
66
|
-
it "raises an error if the actual does not respond to :-" do
|
67
|
-
expect {
|
68
|
-
expect(nil).to be_within(0.1).of(0)
|
69
|
-
}.to raise_error(ArgumentError, /The actual value \(nil\) must respond to `-`/)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
describe "expect(actual).to be_within(delta).percent_of(expected)" do
|
74
|
-
it "passes when actual is within the given percent variance" do
|
75
|
-
expect(9.0).to be_within(10).percent_of(10.0)
|
76
|
-
expect(10.0).to be_within(10).percent_of(10.0)
|
77
|
-
expect(11.0).to be_within(10).percent_of(10.0)
|
78
|
-
end
|
79
|
-
|
80
|
-
it "fails when actual is outside the given percent variance" do
|
81
|
-
expect {
|
82
|
-
expect(8.9).to be_within(10).percent_of(10.0)
|
83
|
-
}.to fail_with("expected 8.9 to be within 10% of 10.0")
|
84
|
-
|
85
|
-
expect {
|
86
|
-
expect(11.1).to be_within(10).percent_of(10.0)
|
87
|
-
}.to fail_with("expected 11.1 to be within 10% of 10.0")
|
88
|
-
end
|
89
|
-
|
90
|
-
it "provides a description" do
|
91
|
-
matcher = be_within(0.5).percent_of(5.0)
|
92
|
-
matcher.matches?(5.1)
|
93
|
-
expect(matcher.description).to eq "be within 0.5% of 5.0"
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
describe "expect(actual).not_to be_within(delta).of(expected)" do
|
98
|
-
it "passes when actual < (expected - delta)" do
|
99
|
-
expect(4.49).not_to be_within(0.5).of(5.0)
|
100
|
-
end
|
101
|
-
|
102
|
-
it "passes when actual > (expected + delta)" do
|
103
|
-
expect(5.51).not_to be_within(0.5).of(5.0)
|
104
|
-
end
|
105
|
-
|
106
|
-
it "fails when actual == expected" do
|
107
|
-
expect {
|
108
|
-
expect(5.0).not_to be_within(0.5).of(5.0)
|
109
|
-
}.to fail_with("expected 5.0 not to be within 0.5 of 5.0")
|
110
|
-
end
|
111
|
-
|
112
|
-
it "fails when actual < (expected + delta)" do
|
113
|
-
expect {
|
114
|
-
expect(5.49).not_to be_within(0.5).of(5.0)
|
115
|
-
}.to fail_with("expected 5.49 not to be within 0.5 of 5.0")
|
116
|
-
end
|
117
|
-
|
118
|
-
it "fails when actual > (expected - delta)" do
|
119
|
-
expect {
|
120
|
-
expect(4.51).not_to be_within(0.5).of(5.0)
|
121
|
-
}.to fail_with("expected 4.51 not to be within 0.5 of 5.0")
|
122
|
-
end
|
123
|
-
|
124
|
-
it "fails when actual == (expected - delta)" do
|
125
|
-
expect {
|
126
|
-
expect(4.5).not_to be_within(0.5).of(5.0)
|
127
|
-
}.to fail_with("expected 4.5 not to be within 0.5 of 5.0")
|
128
|
-
end
|
129
|
-
|
130
|
-
it "fails when actual == (expected + delta)" do
|
131
|
-
expect {
|
132
|
-
expect(5.5).not_to be_within(0.5).of(5.0)
|
133
|
-
}.to fail_with("expected 5.5 not to be within 0.5 of 5.0")
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
@@ -1,553 +0,0 @@
|
|
1
|
-
#Based on patch from Wilson Bilkovich
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
class SomethingExpected
|
6
|
-
attr_accessor :some_value
|
7
|
-
end
|
8
|
-
|
9
|
-
describe "expect { ... }.to change(actual, message)" do
|
10
|
-
context "with a numeric value" do
|
11
|
-
before(:each) do
|
12
|
-
@instance = SomethingExpected.new
|
13
|
-
@instance.some_value = 5
|
14
|
-
end
|
15
|
-
|
16
|
-
it "passes when actual is modified by the block" do
|
17
|
-
expect {@instance.some_value = 6.0}.to change(@instance, :some_value)
|
18
|
-
end
|
19
|
-
|
20
|
-
it "fails when actual is not modified by the block" do
|
21
|
-
expect do
|
22
|
-
expect {}.to change(@instance, :some_value)
|
23
|
-
end.to fail_with("some_value should have changed, but is still 5")
|
24
|
-
end
|
25
|
-
|
26
|
-
it "provides a #description" do
|
27
|
-
expect(change(@instance, :some_value).description).to eq "change #some_value"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context "with boolean values" do
|
32
|
-
before(:each) do
|
33
|
-
@instance = SomethingExpected.new
|
34
|
-
@instance.some_value = true
|
35
|
-
end
|
36
|
-
|
37
|
-
it "passes when actual is modified by the block" do
|
38
|
-
expect {@instance.some_value = false}.to change(@instance, :some_value)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "fails when actual is not modified by the block" do
|
42
|
-
expect do
|
43
|
-
expect {}.to change(@instance, :some_value)
|
44
|
-
end.to fail_with("some_value should have changed, but is still true")
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context "with nil value" do
|
49
|
-
before(:each) do
|
50
|
-
@instance = SomethingExpected.new
|
51
|
-
@instance.some_value = nil
|
52
|
-
end
|
53
|
-
|
54
|
-
it "passes when actual is modified by the block" do
|
55
|
-
expect {@instance.some_value = false}.to change(@instance, :some_value)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "fails when actual is not modified by the block" do
|
59
|
-
expect do
|
60
|
-
expect {}.to change(@instance, :some_value)
|
61
|
-
end.to fail_with("some_value should have changed, but is still nil")
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context "with an array" do
|
66
|
-
before(:each) do
|
67
|
-
@instance = SomethingExpected.new
|
68
|
-
@instance.some_value = []
|
69
|
-
end
|
70
|
-
|
71
|
-
it "passes when actual is modified by the block" do
|
72
|
-
expect {@instance.some_value << 1}.to change(@instance, :some_value)
|
73
|
-
end
|
74
|
-
|
75
|
-
it "fails when a predicate on the actual fails" do
|
76
|
-
expect do
|
77
|
-
expect {@instance.some_value << 1}.to change { @instance.some_value }.to be_empty
|
78
|
-
end.to fail_with(/result should have been changed to/)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "passes when a predicate on the actual passes" do
|
82
|
-
@instance.some_value = [1]
|
83
|
-
expect {@instance.some_value.pop}.to change { @instance.some_value }.to be_empty
|
84
|
-
end
|
85
|
-
|
86
|
-
it "fails when actual is not modified by the block" do
|
87
|
-
expect do
|
88
|
-
expect {}.to change(@instance, :some_value)
|
89
|
-
end.to fail_with("some_value should have changed, but is still []")
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
context "with a hash" do
|
94
|
-
before(:each) do
|
95
|
-
@instance = SomethingExpected.new
|
96
|
-
@instance.some_value = {:a => 'a'}
|
97
|
-
end
|
98
|
-
|
99
|
-
it "passes when actual is modified by the block" do
|
100
|
-
expect {@instance.some_value[:a] = 'A'}.to change(@instance, :some_value)
|
101
|
-
end
|
102
|
-
|
103
|
-
it "fails when actual is not modified by the block" do
|
104
|
-
expect do
|
105
|
-
expect {}.to change(@instance, :some_value)
|
106
|
-
end.to fail
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
context "with a string" do
|
111
|
-
it "passes when actual is modified by the block" do
|
112
|
-
string = "ab"
|
113
|
-
expect { string << "c" }.to change { string }
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'fails when actual is not modified by the block' do
|
117
|
-
string = "ab"
|
118
|
-
expect {
|
119
|
-
expect { }.to change { string }
|
120
|
-
}.to fail_with(/should have changed/)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
context "with an arbitrary enumerable" do
|
125
|
-
before(:each) do
|
126
|
-
@instance = SomethingExpected.new
|
127
|
-
@instance.some_value = Class.new do
|
128
|
-
include Enumerable
|
129
|
-
|
130
|
-
attr_reader :elements
|
131
|
-
|
132
|
-
def initialize(*elements)
|
133
|
-
@elements = elements.dup
|
134
|
-
end
|
135
|
-
|
136
|
-
def <<(element)
|
137
|
-
elements << element
|
138
|
-
end
|
139
|
-
|
140
|
-
def dup
|
141
|
-
self.class.new(*elements)
|
142
|
-
end
|
143
|
-
|
144
|
-
def ==(other)
|
145
|
-
elements == other.elements
|
146
|
-
end
|
147
|
-
end.new
|
148
|
-
end
|
149
|
-
|
150
|
-
it "passes when actual is modified by the block" do
|
151
|
-
expect {@instance.some_value << 1}.to change(@instance, :some_value)
|
152
|
-
end
|
153
|
-
|
154
|
-
it "fails when actual is not modified by the block" do
|
155
|
-
expect do
|
156
|
-
expect {}.to change(@instance, :some_value)
|
157
|
-
end.to fail_with(/^some_value should have changed, but is still/)
|
158
|
-
end
|
159
|
-
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
describe "expect { ... }.not_to change(actual, message)" do
|
164
|
-
before(:each) do
|
165
|
-
@instance = SomethingExpected.new
|
166
|
-
@instance.some_value = 5
|
167
|
-
end
|
168
|
-
|
169
|
-
it "passes when actual is not modified by the block" do
|
170
|
-
expect { }.not_to change(@instance, :some_value)
|
171
|
-
end
|
172
|
-
|
173
|
-
it "fails when actual is not modified by the block" do
|
174
|
-
expect do
|
175
|
-
expect {@instance.some_value = 6}.not_to change(@instance, :some_value)
|
176
|
-
end.to fail_with("some_value should not have changed, but did change from 5 to 6")
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
describe "expect { ... }.to change { block }" do
|
181
|
-
o = SomethingExpected.new
|
182
|
-
it_behaves_like "an RSpec matcher", :valid_value => lambda { o.some_value = 5 },
|
183
|
-
:invalid_value => lambda { } do
|
184
|
-
let(:matcher) { change { o.some_value } }
|
185
|
-
end
|
186
|
-
|
187
|
-
before(:each) do
|
188
|
-
@instance = SomethingExpected.new
|
189
|
-
@instance.some_value = 5
|
190
|
-
end
|
191
|
-
|
192
|
-
it "passes when actual is modified by the block" do
|
193
|
-
expect {@instance.some_value = 6}.to change { @instance.some_value }
|
194
|
-
end
|
195
|
-
|
196
|
-
it "fails when actual is not modified by the block" do
|
197
|
-
expect do
|
198
|
-
expect {}.to change{ @instance.some_value }
|
199
|
-
end.to fail_with("result should have changed, but is still 5")
|
200
|
-
end
|
201
|
-
|
202
|
-
it "warns if passed a block using do/end instead of {}" do
|
203
|
-
expect do
|
204
|
-
expect {}.to change do; end
|
205
|
-
end.to raise_error(SyntaxError, /block passed to should or should_not/)
|
206
|
-
end
|
207
|
-
|
208
|
-
it "provides a #description" do
|
209
|
-
expect(change { @instance.some_value }.description).to eq "change #result"
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
describe "expect { ... }.not_to change { block }" do
|
214
|
-
before(:each) do
|
215
|
-
@instance = SomethingExpected.new
|
216
|
-
@instance.some_value = 5
|
217
|
-
end
|
218
|
-
|
219
|
-
it "passes when actual is modified by the block" do
|
220
|
-
expect {}.not_to change{ @instance.some_value }
|
221
|
-
end
|
222
|
-
|
223
|
-
it "fails when actual is not modified by the block" do
|
224
|
-
expect do
|
225
|
-
expect {@instance.some_value = 6}.not_to change { @instance.some_value }
|
226
|
-
end.to fail_with("result should not have changed, but did change from 5 to 6")
|
227
|
-
end
|
228
|
-
|
229
|
-
it "warns if passed a block using do/end instead of {}" do
|
230
|
-
expect do
|
231
|
-
expect {}.not_to change do; end
|
232
|
-
end.to raise_error(SyntaxError, /block passed to should or should_not/)
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
describe "expect { ... }.to change(actual, message).by(expected)" do
|
237
|
-
before(:each) do
|
238
|
-
@instance = SomethingExpected.new
|
239
|
-
@instance.some_value = 5
|
240
|
-
end
|
241
|
-
|
242
|
-
it "passes when attribute is changed by expected amount" do
|
243
|
-
expect { @instance.some_value += 1 }.to change(@instance, :some_value).by(1)
|
244
|
-
end
|
245
|
-
|
246
|
-
it "passes when attribute is not changed and expected amount is 0" do
|
247
|
-
expect { @instance.some_value += 0 }.to change(@instance, :some_value).by(0)
|
248
|
-
end
|
249
|
-
|
250
|
-
it "fails when the attribute is changed by unexpected amount" do
|
251
|
-
expect do
|
252
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by(1)
|
253
|
-
end.to fail_with("some_value should have been changed by 1, but was changed by 2")
|
254
|
-
end
|
255
|
-
|
256
|
-
it "fails when the attribute is changed by unexpected amount in the opposite direction" do
|
257
|
-
expect do
|
258
|
-
expect { @instance.some_value -= 1 }.to change(@instance, :some_value).by(1)
|
259
|
-
end.to fail_with("some_value should have been changed by 1, but was changed by -1")
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
describe "expect { ... }.to change { block }.by(expected)" do
|
264
|
-
before(:each) do
|
265
|
-
@instance = SomethingExpected.new
|
266
|
-
@instance.some_value = 5
|
267
|
-
end
|
268
|
-
|
269
|
-
it "passes when attribute is changed by expected amount" do
|
270
|
-
expect { @instance.some_value += 1 }.to change{@instance.some_value}.by(1)
|
271
|
-
end
|
272
|
-
|
273
|
-
it "fails when the attribute is changed by unexpected amount" do
|
274
|
-
expect do
|
275
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by(1)
|
276
|
-
end.to fail_with("result should have been changed by 1, but was changed by 2")
|
277
|
-
end
|
278
|
-
|
279
|
-
it "fails when the attribute is changed by unexpected amount in the opposite direction" do
|
280
|
-
expect do
|
281
|
-
expect { @instance.some_value -= 1 }.to change{@instance.some_value}.by(1)
|
282
|
-
end.to fail_with("result should have been changed by 1, but was changed by -1")
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
describe "expect { ... }.to change(actual, message).by_at_least(expected)" do
|
287
|
-
before(:each) do
|
288
|
-
@instance = SomethingExpected.new
|
289
|
-
@instance.some_value = 5
|
290
|
-
end
|
291
|
-
|
292
|
-
it "passes when attribute is changed by greater than the expected amount" do
|
293
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_least(1)
|
294
|
-
end
|
295
|
-
|
296
|
-
it "passes when attribute is changed by the expected amount" do
|
297
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_least(2)
|
298
|
-
end
|
299
|
-
|
300
|
-
it "fails when the attribute is changed by less than the expected amount" do
|
301
|
-
expect do
|
302
|
-
expect { @instance.some_value += 1 }.to change(@instance, :some_value).by_at_least(2)
|
303
|
-
end.to fail_with("some_value should have been changed by at least 2, but was changed by 1")
|
304
|
-
end
|
305
|
-
|
306
|
-
end
|
307
|
-
|
308
|
-
describe "expect { ... }.to change { block }.by_at_least(expected)" do
|
309
|
-
before(:each) do
|
310
|
-
@instance = SomethingExpected.new
|
311
|
-
@instance.some_value = 5
|
312
|
-
end
|
313
|
-
|
314
|
-
it "passes when attribute is changed by greater than expected amount" do
|
315
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_least(1)
|
316
|
-
end
|
317
|
-
|
318
|
-
it "passes when attribute is changed by the expected amount" do
|
319
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_least(2)
|
320
|
-
end
|
321
|
-
|
322
|
-
it "fails when the attribute is changed by less than the unexpected amount" do
|
323
|
-
expect do
|
324
|
-
expect { @instance.some_value += 1 }.to change{@instance.some_value}.by_at_least(2)
|
325
|
-
end.to fail_with("result should have been changed by at least 2, but was changed by 1")
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
|
330
|
-
describe "expect { ... }.to change(actual, message).by_at_most(expected)" do
|
331
|
-
before(:each) do
|
332
|
-
@instance = SomethingExpected.new
|
333
|
-
@instance.some_value = 5
|
334
|
-
end
|
335
|
-
|
336
|
-
it "passes when attribute is changed by less than the expected amount" do
|
337
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(3)
|
338
|
-
end
|
339
|
-
|
340
|
-
it "passes when attribute is changed by the expected amount" do
|
341
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(2)
|
342
|
-
end
|
343
|
-
|
344
|
-
it "fails when the attribute is changed by greater than the expected amount" do
|
345
|
-
expect do
|
346
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(1)
|
347
|
-
end.to fail_with("some_value should have been changed by at most 1, but was changed by 2")
|
348
|
-
end
|
349
|
-
|
350
|
-
end
|
351
|
-
|
352
|
-
describe "expect { ... }.to change { block }.by_at_most(expected)" do
|
353
|
-
before(:each) do
|
354
|
-
@instance = SomethingExpected.new
|
355
|
-
@instance.some_value = 5
|
356
|
-
end
|
357
|
-
|
358
|
-
it "passes when attribute is changed by less than expected amount" do
|
359
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_most(3)
|
360
|
-
end
|
361
|
-
|
362
|
-
it "passes when attribute is changed by the expected amount" do
|
363
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_most(2)
|
364
|
-
end
|
365
|
-
|
366
|
-
it "fails when the attribute is changed by greater than the unexpected amount" do
|
367
|
-
expect do
|
368
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_most(1)
|
369
|
-
end.to fail_with("result should have been changed by at most 1, but was changed by 2")
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
describe "expect { ... }.to change(actual, message).from(old)" do
|
374
|
-
context "with boolean values" do
|
375
|
-
before(:each) do
|
376
|
-
@instance = SomethingExpected.new
|
377
|
-
@instance.some_value = true
|
378
|
-
end
|
379
|
-
|
380
|
-
it "passes when attribute is == to expected value before executing block" do
|
381
|
-
expect { @instance.some_value = false }.to change(@instance, :some_value).from(true)
|
382
|
-
end
|
383
|
-
|
384
|
-
it "fails when attribute is not == to expected value before executing block" do
|
385
|
-
expect do
|
386
|
-
expect { @instance.some_value = 'foo' }.to change(@instance, :some_value).from(false)
|
387
|
-
end.to fail_with("some_value should have initially been false, but was true")
|
388
|
-
end
|
389
|
-
end
|
390
|
-
context "with non-boolean values" do
|
391
|
-
before(:each) do
|
392
|
-
@instance = SomethingExpected.new
|
393
|
-
@instance.some_value = 'string'
|
394
|
-
end
|
395
|
-
|
396
|
-
it "passes when attribute is === to expected value before executing block" do
|
397
|
-
expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from("string")
|
398
|
-
end
|
399
|
-
|
400
|
-
it "compares the expected and actual values with ===" do
|
401
|
-
expected = "string"
|
402
|
-
expected.should_receive(:===).and_return true
|
403
|
-
expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from(expected)
|
404
|
-
end
|
405
|
-
|
406
|
-
it "fails when attribute is not === to expected value before executing block" do
|
407
|
-
expect do
|
408
|
-
expect { @instance.some_value = "knot" }.to change(@instance, :some_value).from("cat")
|
409
|
-
end.to fail_with("some_value should have initially been \"cat\", but was \"string\"")
|
410
|
-
end
|
411
|
-
end
|
412
|
-
end
|
413
|
-
|
414
|
-
describe "expect { ... }.to change { block }.from(old)" do
|
415
|
-
before(:each) do
|
416
|
-
@instance = SomethingExpected.new
|
417
|
-
@instance.some_value = 'string'
|
418
|
-
end
|
419
|
-
|
420
|
-
it "passes when attribute is === to expected value before executing block" do
|
421
|
-
expect { @instance.some_value = "astring" }.to change{@instance.some_value}.from("string")
|
422
|
-
end
|
423
|
-
|
424
|
-
it "compares the expected and actual values with ===" do
|
425
|
-
expected = "string"
|
426
|
-
expected.should_receive(:===).and_return true
|
427
|
-
expect { @instance.some_value = "astring" }.to change{@instance.some_value}.from(expected)
|
428
|
-
end
|
429
|
-
|
430
|
-
it "fails when attribute is not === to expected value before executing block" do
|
431
|
-
expect do
|
432
|
-
expect { @instance.some_value = "knot" }.to change{@instance.some_value}.from("cat")
|
433
|
-
end.to fail_with("result should have initially been \"cat\", but was \"string\"")
|
434
|
-
end
|
435
|
-
end
|
436
|
-
|
437
|
-
describe "expect { ... }.to change(actual, message).to(new)" do
|
438
|
-
context "with boolean values" do
|
439
|
-
before(:each) do
|
440
|
-
@instance = SomethingExpected.new
|
441
|
-
@instance.some_value = true
|
442
|
-
end
|
443
|
-
|
444
|
-
it "passes when attribute is == to expected value after executing block" do
|
445
|
-
expect { @instance.some_value = false }.to change(@instance, :some_value).to(false)
|
446
|
-
end
|
447
|
-
|
448
|
-
it "fails when attribute is not == to expected value after executing block" do
|
449
|
-
expect do
|
450
|
-
expect { @instance.some_value = 1 }.to change(@instance, :some_value).from(true).to(false)
|
451
|
-
end.to fail_with("some_value should have been changed to false, but is now 1")
|
452
|
-
end
|
453
|
-
end
|
454
|
-
context "with non-boolean values" do
|
455
|
-
before(:each) do
|
456
|
-
@instance = SomethingExpected.new
|
457
|
-
@instance.some_value = 'string'
|
458
|
-
end
|
459
|
-
|
460
|
-
it "passes when attribute is === to expected value after executing block" do
|
461
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat")
|
462
|
-
end
|
463
|
-
|
464
|
-
it "compares the expected and actual values with ===" do
|
465
|
-
expected = "cat"
|
466
|
-
expected.should_receive(:===).and_return true
|
467
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to(expected)
|
468
|
-
end
|
469
|
-
|
470
|
-
it "fails when attribute is not === to expected value after executing block" do
|
471
|
-
expect do
|
472
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog")
|
473
|
-
end.to fail_with("some_value should have been changed to \"dog\", but is now \"cat\"")
|
474
|
-
end
|
475
|
-
end
|
476
|
-
end
|
477
|
-
|
478
|
-
describe "expect { ... }.to change { block }.to(new)" do
|
479
|
-
before(:each) do
|
480
|
-
@instance = SomethingExpected.new
|
481
|
-
@instance.some_value = 'string'
|
482
|
-
end
|
483
|
-
|
484
|
-
it "passes when attribute is === to expected value after executing block" do
|
485
|
-
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to("cat")
|
486
|
-
end
|
487
|
-
|
488
|
-
it "compares the expected and actual values with ===" do
|
489
|
-
expected = "cat"
|
490
|
-
expected.should_receive(:===).and_return true
|
491
|
-
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to(expected)
|
492
|
-
end
|
493
|
-
|
494
|
-
it "fails when attribute is not === to expected value after executing block" do
|
495
|
-
expect do
|
496
|
-
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.from("string").to("dog")
|
497
|
-
end.to fail_with("result should have been changed to \"dog\", but is now \"cat\"")
|
498
|
-
end
|
499
|
-
end
|
500
|
-
|
501
|
-
describe "expect { ... }.to change(actual, message).from(old).to(new)" do
|
502
|
-
before(:each) do
|
503
|
-
@instance = SomethingExpected.new
|
504
|
-
@instance.some_value = 'string'
|
505
|
-
end
|
506
|
-
|
507
|
-
it "passes when #to comes before #from" do
|
508
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat").from("string")
|
509
|
-
end
|
510
|
-
|
511
|
-
it "passes when #from comes before #to" do
|
512
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("cat")
|
513
|
-
end
|
514
|
-
|
515
|
-
it "shows the correct messaging when #after and #to are different" do
|
516
|
-
expect do
|
517
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog")
|
518
|
-
end.to fail_with("some_value should have been changed to \"dog\", but is now \"cat\"")
|
519
|
-
end
|
520
|
-
|
521
|
-
it "shows the correct messaging when #before and #from are different" do
|
522
|
-
expect do
|
523
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("not_string").to("cat")
|
524
|
-
end.to fail_with("some_value should have initially been \"not_string\", but was \"string\"")
|
525
|
-
end
|
526
|
-
end
|
527
|
-
|
528
|
-
describe "expect { ... }.to change { block }.from(old).to(new)" do
|
529
|
-
before(:each) do
|
530
|
-
@instance = SomethingExpected.new
|
531
|
-
@instance.some_value = 'string'
|
532
|
-
end
|
533
|
-
|
534
|
-
it "passes when #to comes before #from" do
|
535
|
-
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to("cat").from("string")
|
536
|
-
end
|
537
|
-
|
538
|
-
it "passes when #from comes before #to" do
|
539
|
-
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.from("string").to("cat")
|
540
|
-
end
|
541
|
-
end
|
542
|
-
|
543
|
-
describe RSpec::Matchers::BuiltIn::Change do
|
544
|
-
it "works when the receiver has implemented #send" do
|
545
|
-
@instance = SomethingExpected.new
|
546
|
-
@instance.some_value = "string"
|
547
|
-
def @instance.send(*args); raise "DOH! Library developers shouldn't use #send!" end
|
548
|
-
|
549
|
-
expect {
|
550
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value)
|
551
|
-
}.not_to raise_error
|
552
|
-
end
|
553
|
-
end
|