rspec-expectations 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 -2
- data/.yardopts +0 -1
- data/Changelog.md +115 -35
- data/README.md +2 -2
- data/lib/rspec/expectations.rb +13 -8
- data/lib/rspec/{matchers → expectations}/configuration.rb +38 -13
- data/lib/rspec/expectations/expectation_target.rb +72 -8
- data/lib/rspec/expectations/fail_with.rb +10 -52
- data/lib/rspec/expectations/handler.rb +9 -11
- data/lib/rspec/expectations/syntax.rb +37 -35
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +60 -9
- data/lib/rspec/matchers/aliased_matcher.rb +6 -0
- data/lib/rspec/matchers/built_in.rb +9 -1
- data/lib/rspec/matchers/built_in/all.rb +78 -0
- data/lib/rspec/matchers/built_in/base_matcher.rb +39 -1
- data/lib/rspec/matchers/built_in/be.rb +117 -42
- data/lib/rspec/matchers/built_in/be_between.rb +22 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +11 -3
- data/lib/rspec/matchers/built_in/be_kind_of.rb +5 -0
- data/lib/rspec/matchers/built_in/be_within.rb +26 -6
- data/lib/rspec/matchers/built_in/change.rb +89 -13
- data/lib/rspec/matchers/built_in/compound.rb +19 -3
- data/lib/rspec/matchers/built_in/contain_exactly.rb +17 -6
- data/lib/rspec/matchers/built_in/cover.rb +3 -0
- data/lib/rspec/matchers/built_in/eq.rb +20 -5
- data/lib/rspec/matchers/built_in/eql.rb +15 -3
- data/lib/rspec/matchers/built_in/equal.rb +23 -6
- data/lib/rspec/matchers/built_in/exist.rb +74 -10
- data/lib/rspec/matchers/built_in/has.rb +58 -3
- data/lib/rspec/matchers/built_in/include.rb +16 -1
- data/lib/rspec/matchers/built_in/match.rb +14 -4
- data/lib/rspec/matchers/built_in/operators.rb +16 -0
- data/lib/rspec/matchers/built_in/output.rb +47 -5
- data/lib/rspec/matchers/built_in/raise_error.rb +40 -23
- data/lib/rspec/matchers/built_in/respond_to.rb +37 -16
- data/lib/rspec/matchers/built_in/satisfy.rb +15 -0
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +29 -14
- data/lib/rspec/matchers/built_in/throw_symbol.rb +32 -3
- data/lib/rspec/matchers/built_in/yield.rb +148 -44
- data/lib/rspec/matchers/composable.rb +48 -7
- data/lib/rspec/matchers/dsl.rb +45 -17
- data/lib/rspec/matchers/generated_descriptions.rb +7 -0
- data/lib/rspec/matchers/matcher_delegator.rb +6 -2
- data/lib/rspec/matchers/pretty.rb +15 -19
- metadata +33 -236
- metadata.gz.sig +0 -0
- data/features/README.md +0 -48
- data/features/Upgrade.md +0 -53
- data/features/built_in_matchers/README.md +0 -96
- data/features/built_in_matchers/be.feature +0 -175
- data/features/built_in_matchers/be_within.feature +0 -48
- data/features/built_in_matchers/comparisons.feature +0 -97
- data/features/built_in_matchers/contain_exactly.feature +0 -46
- 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 -136
- 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/include.feature +0 -126
- data/features/built_in_matchers/match.feature +0 -51
- data/features/built_in_matchers/output.feature +0 -70
- data/features/built_in_matchers/predicates.feature +0 -161
- 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/composing_matchers.feature +0 -250
- data/features/compound_expectations.feature +0 -45
- 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 -340
- data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -34
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
- data/features/customized_message.feature +0 -39
- 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 -21
- data/features/support/rubinius.rb +0 -6
- data/features/syntax_configuration.feature +0 -71
- data/features/test_frameworks/minitest.feature +0 -44
- data/lib/rspec-expectations.rb +0 -1
- data/lib/rspec/expectations/diff_presenter.rb +0 -141
- data/lib/rspec/expectations/differ.rb +0 -44
- data/lib/rspec/expectations/encoded_string.rb +0 -56
- data/spec/rspec/expectations/diff_presenter_spec.rb +0 -249
- data/spec/rspec/expectations/encoded_string_spec.rb +0 -74
- 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 -205
- data/spec/rspec/expectations/minitest_integration_spec.rb +0 -27
- data/spec/rspec/expectations/syntax_spec.rb +0 -89
- data/spec/rspec/expectations_spec.rb +0 -12
- data/spec/rspec/matchers/aliased_matcher_spec.rb +0 -48
- data/spec/rspec/matchers/aliases_spec.rb +0 -449
- data/spec/rspec/matchers/built_in/base_matcher_spec.rb +0 -83
- data/spec/rspec/matchers/built_in/be_between_spec.rb +0 -159
- data/spec/rspec/matchers/built_in/be_instance_of_spec.rb +0 -63
- data/spec/rspec/matchers/built_in/be_kind_of_spec.rb +0 -41
- data/spec/rspec/matchers/built_in/be_spec.rb +0 -592
- data/spec/rspec/matchers/built_in/be_within_spec.rb +0 -141
- data/spec/rspec/matchers/built_in/change_spec.rb +0 -808
- data/spec/rspec/matchers/built_in/compound_spec.rb +0 -292
- data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +0 -441
- data/spec/rspec/matchers/built_in/cover_spec.rb +0 -69
- data/spec/rspec/matchers/built_in/eq_spec.rb +0 -156
- data/spec/rspec/matchers/built_in/eql_spec.rb +0 -41
- data/spec/rspec/matchers/built_in/equal_spec.rb +0 -106
- data/spec/rspec/matchers/built_in/exist_spec.rb +0 -124
- data/spec/rspec/matchers/built_in/has_spec.rb +0 -161
- data/spec/rspec/matchers/built_in/include_spec.rb +0 -540
- data/spec/rspec/matchers/built_in/match_spec.rb +0 -102
- data/spec/rspec/matchers/built_in/operators_spec.rb +0 -252
- data/spec/rspec/matchers/built_in/output_spec.rb +0 -165
- data/spec/rspec/matchers/built_in/raise_error_spec.rb +0 -461
- data/spec/rspec/matchers/built_in/respond_to_spec.rb +0 -292
- data/spec/rspec/matchers/built_in/satisfy_spec.rb +0 -44
- data/spec/rspec/matchers/built_in/start_and_end_with_spec.rb +0 -253
- data/spec/rspec/matchers/built_in/throw_symbol_spec.rb +0 -135
- data/spec/rspec/matchers/built_in/yield_spec.rb +0 -627
- data/spec/rspec/matchers/configuration_spec.rb +0 -213
- data/spec/rspec/matchers/description_generation_spec.rb +0 -191
- data/spec/rspec/matchers/dsl_spec.rb +0 -895
- data/spec/rspec/matchers/legacy_spec.rb +0 -101
- data/spec/rspec/matchers_spec.rb +0 -74
- data/spec/spec_helper.rb +0 -85
- data/spec/support/matchers.rb +0 -22
- data/spec/support/shared_examples.rb +0 -35
@@ -1,141 +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 "fails if the actual is not numeric" do
|
67
|
-
expect {
|
68
|
-
expect(nil).to be_within(0.1).of(0)
|
69
|
-
}.to fail_with("expected nil to be within 0.1 of 0, but it could not be treated as a numeric value")
|
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
|
-
|
136
|
-
it "passes if the actual is not numeric" do
|
137
|
-
expect(nil).not_to be_within(0.1).of(0)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
@@ -1,808 +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("expected #some_value to 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
|
-
it "can specify the change of a variable's class" do
|
32
|
-
val = nil
|
33
|
-
|
34
|
-
expect {
|
35
|
-
val = 42
|
36
|
-
}.to change { val.class }.from(NilClass).to(Fixnum)
|
37
|
-
|
38
|
-
expect {
|
39
|
-
expect {
|
40
|
-
val = "string"
|
41
|
-
}.to change { val.class }.from(Fixnum).to(NilClass)
|
42
|
-
}.to fail_with(/but is now String/)
|
43
|
-
end
|
44
|
-
|
45
|
-
context "with boolean values" do
|
46
|
-
before(:each) do
|
47
|
-
@instance = SomethingExpected.new
|
48
|
-
@instance.some_value = true
|
49
|
-
end
|
50
|
-
|
51
|
-
it "passes when actual is modified by the block" do
|
52
|
-
expect {@instance.some_value = false}.to change(@instance, :some_value)
|
53
|
-
end
|
54
|
-
|
55
|
-
it "fails when actual is not modified by the block" do
|
56
|
-
expect do
|
57
|
-
expect {}.to change(@instance, :some_value)
|
58
|
-
end.to fail_with("expected #some_value to have changed, but is still true")
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context "with nil value" do
|
63
|
-
before(:each) do
|
64
|
-
@instance = SomethingExpected.new
|
65
|
-
@instance.some_value = nil
|
66
|
-
end
|
67
|
-
|
68
|
-
it "passes when actual is modified by the block" do
|
69
|
-
expect {@instance.some_value = false}.to change(@instance, :some_value)
|
70
|
-
end
|
71
|
-
|
72
|
-
it "fails when actual is not modified by the block" do
|
73
|
-
expect do
|
74
|
-
expect {}.to change(@instance, :some_value)
|
75
|
-
end.to fail_with("expected #some_value to have changed, but is still nil")
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
context "with an array" do
|
80
|
-
before(:each) do
|
81
|
-
@instance = SomethingExpected.new
|
82
|
-
@instance.some_value = []
|
83
|
-
end
|
84
|
-
|
85
|
-
it "passes when actual is modified by the block" do
|
86
|
-
expect {@instance.some_value << 1}.to change(@instance, :some_value)
|
87
|
-
end
|
88
|
-
|
89
|
-
it "fails when a predicate on the actual fails" do
|
90
|
-
expect do
|
91
|
-
expect {@instance.some_value << 1}.to change { @instance.some_value }.to be_empty
|
92
|
-
end.to fail_with(/result to have changed to/)
|
93
|
-
end
|
94
|
-
|
95
|
-
it "passes when a predicate on the actual passes" do
|
96
|
-
@instance.some_value = [1]
|
97
|
-
expect {@instance.some_value.pop}.to change { @instance.some_value }.to be_empty
|
98
|
-
end
|
99
|
-
|
100
|
-
it "fails when actual is not modified by the block" do
|
101
|
-
expect do
|
102
|
-
expect {}.to change(@instance, :some_value)
|
103
|
-
end.to fail_with("expected #some_value to have changed, but is still []")
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
context "with a hash" do
|
108
|
-
before(:each) do
|
109
|
-
@instance = SomethingExpected.new
|
110
|
-
@instance.some_value = {:a => 'a'}
|
111
|
-
end
|
112
|
-
|
113
|
-
it "passes when actual is modified by the block" do
|
114
|
-
expect {@instance.some_value[:a] = 'A'}.to change(@instance, :some_value)
|
115
|
-
end
|
116
|
-
|
117
|
-
it "fails when actual is not modified by the block" do
|
118
|
-
expect do
|
119
|
-
expect {}.to change(@instance, :some_value)
|
120
|
-
end.to fail
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
context "with a string" do
|
125
|
-
it "passes when actual is modified by the block" do
|
126
|
-
string = "ab"
|
127
|
-
expect { string << "c" }.to change { string }
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'fails when actual is not modified by the block' do
|
131
|
-
string = "ab"
|
132
|
-
expect {
|
133
|
-
expect { }.to change { string }
|
134
|
-
}.to fail_with(/to have changed/)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
context "with an arbitrary enumerable" do
|
139
|
-
before(:each) do
|
140
|
-
@instance = SomethingExpected.new
|
141
|
-
@instance.some_value = Class.new do
|
142
|
-
include Enumerable
|
143
|
-
|
144
|
-
attr_reader :elements
|
145
|
-
|
146
|
-
def initialize(*elements)
|
147
|
-
@elements = elements.dup
|
148
|
-
end
|
149
|
-
|
150
|
-
def <<(element)
|
151
|
-
elements << element
|
152
|
-
end
|
153
|
-
|
154
|
-
def dup
|
155
|
-
self.class.new(*elements)
|
156
|
-
end
|
157
|
-
|
158
|
-
def ==(other)
|
159
|
-
elements == other.elements
|
160
|
-
end
|
161
|
-
end.new
|
162
|
-
end
|
163
|
-
|
164
|
-
it "passes when actual is modified by the block" do
|
165
|
-
expect {@instance.some_value << 1}.to change(@instance, :some_value)
|
166
|
-
end
|
167
|
-
|
168
|
-
it "fails when actual is not modified by the block" do
|
169
|
-
expect do
|
170
|
-
expect {}.to change(@instance, :some_value)
|
171
|
-
end.to fail_with(/^expected #some_value to have changed, but is still/)
|
172
|
-
end
|
173
|
-
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
describe "expect { ... }.not_to change(actual, message)" do
|
178
|
-
before(:each) do
|
179
|
-
@instance = SomethingExpected.new
|
180
|
-
@instance.some_value = 5
|
181
|
-
end
|
182
|
-
|
183
|
-
it "passes when actual is not modified by the block" do
|
184
|
-
expect { }.not_to change(@instance, :some_value)
|
185
|
-
end
|
186
|
-
|
187
|
-
it "fails when actual is not modified by the block" do
|
188
|
-
expect do
|
189
|
-
expect {@instance.some_value = 6}.not_to change(@instance, :some_value)
|
190
|
-
end.to fail_with("expected #some_value not to have changed, but did change from 5 to 6")
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
describe "expect { ... }.to change { block }" do
|
195
|
-
|
196
|
-
before(:each) do
|
197
|
-
@instance = SomethingExpected.new
|
198
|
-
@instance.some_value = 5
|
199
|
-
end
|
200
|
-
|
201
|
-
it "passes when actual is modified by the block" do
|
202
|
-
expect {@instance.some_value = 6}.to change { @instance.some_value }
|
203
|
-
end
|
204
|
-
|
205
|
-
it "fails when actual is not modified by the block" do
|
206
|
-
expect do
|
207
|
-
expect {}.to change{ @instance.some_value }
|
208
|
-
end.to fail_with("expected result to have changed, but is still 5")
|
209
|
-
end
|
210
|
-
|
211
|
-
it "warns if passed a block using do/end instead of {}" do
|
212
|
-
expect do
|
213
|
-
expect {}.to change do; end
|
214
|
-
end.to raise_error(SyntaxError, /block passed to the `change` matcher/)
|
215
|
-
end
|
216
|
-
|
217
|
-
it "provides a #description" do
|
218
|
-
expect(change { @instance.some_value }.description).to eq "change result"
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
describe "expect { ... }.not_to change { block }" do
|
223
|
-
before(:each) do
|
224
|
-
@instance = SomethingExpected.new
|
225
|
-
@instance.some_value = 5
|
226
|
-
end
|
227
|
-
|
228
|
-
it "passes when actual is modified by the block" do
|
229
|
-
expect {}.not_to change{ @instance.some_value }
|
230
|
-
end
|
231
|
-
|
232
|
-
it "fails when actual is not modified by the block" do
|
233
|
-
expect do
|
234
|
-
expect {@instance.some_value = 6}.not_to change { @instance.some_value }
|
235
|
-
end.to fail_with("expected result not to have changed, but did change from 5 to 6")
|
236
|
-
end
|
237
|
-
|
238
|
-
it "warns if passed a block using do/end instead of {}" do
|
239
|
-
expect do
|
240
|
-
expect {}.not_to change do; end
|
241
|
-
end.to raise_error(SyntaxError, /block passed to the `change` matcher/)
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
|
246
|
-
describe "expect { ... }.not_to change { }.from" do
|
247
|
-
context 'when the value starts at the from value' do
|
248
|
-
it 'passes when the value does not change' do
|
249
|
-
k = 5
|
250
|
-
expect { }.not_to change { k }.from(5)
|
251
|
-
end
|
252
|
-
|
253
|
-
it 'fails when the value does change' do
|
254
|
-
expect {
|
255
|
-
k = 5
|
256
|
-
expect { k += 1 }.not_to change { k }.from(5)
|
257
|
-
}.to fail_with(/but did change from 5 to 6/)
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
context 'when the value starts at a different value' do
|
262
|
-
it 'fails when the value does not change' do
|
263
|
-
expect {
|
264
|
-
k = 6
|
265
|
-
expect { }.not_to change { k }.from(5)
|
266
|
-
}.to fail_with(/expected result to have initially been 5/)
|
267
|
-
end
|
268
|
-
|
269
|
-
it 'fails when the value does change' do
|
270
|
-
expect {
|
271
|
-
k = 6
|
272
|
-
expect { k += 1 }.not_to change { k }.from(5)
|
273
|
-
}.to fail_with(/expected result to have initially been 5/)
|
274
|
-
end
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
describe "expect { ... }.not_to change { }.to" do
|
279
|
-
it 'is not supported' do
|
280
|
-
expect {
|
281
|
-
expect { }.not_to change { }.to(3)
|
282
|
-
}.to raise_error(NotImplementedError)
|
283
|
-
end
|
284
|
-
|
285
|
-
it 'is not supported when it comes after `from`' do
|
286
|
-
expect {
|
287
|
-
expect { }.not_to change { }.from(nil).to(3)
|
288
|
-
}.to raise_error(NotImplementedError)
|
289
|
-
end
|
290
|
-
end
|
291
|
-
|
292
|
-
describe "expect { ... }.not_to change { }.by" do
|
293
|
-
it 'is not supported' do
|
294
|
-
expect {
|
295
|
-
expect { }.not_to change { }.by(3)
|
296
|
-
}.to raise_error(NotImplementedError)
|
297
|
-
end
|
298
|
-
end
|
299
|
-
|
300
|
-
describe "expect { ... }.not_to change { }.by_at_least" do
|
301
|
-
it 'is not supported' do
|
302
|
-
expect {
|
303
|
-
expect { }.not_to change { }.by_at_least(3)
|
304
|
-
}.to raise_error(NotImplementedError)
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
describe "expect { ... }.not_to change { }.by_at_most" do
|
309
|
-
it 'is not supported' do
|
310
|
-
expect {
|
311
|
-
expect { }.not_to change { }.by_at_most(3)
|
312
|
-
}.to raise_error(NotImplementedError)
|
313
|
-
end
|
314
|
-
end
|
315
|
-
|
316
|
-
describe "expect { ... }.to change(actual, message).by(expected)" do
|
317
|
-
before(:each) do
|
318
|
-
@instance = SomethingExpected.new
|
319
|
-
@instance.some_value = 5
|
320
|
-
end
|
321
|
-
|
322
|
-
it "passes when attribute is changed by expected amount" do
|
323
|
-
expect { @instance.some_value += 1 }.to change(@instance, :some_value).by(1)
|
324
|
-
end
|
325
|
-
|
326
|
-
it "passes when attribute is not changed and expected amount is 0" do
|
327
|
-
expect { @instance.some_value += 0 }.to change(@instance, :some_value).by(0)
|
328
|
-
end
|
329
|
-
|
330
|
-
it "fails when the attribute is changed by unexpected amount" do
|
331
|
-
expect do
|
332
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by(1)
|
333
|
-
end.to fail_with("expected #some_value to have changed by 1, but was changed by 2")
|
334
|
-
end
|
335
|
-
|
336
|
-
it "fails when the attribute is changed by unexpected amount in the opposite direction" do
|
337
|
-
expect do
|
338
|
-
expect { @instance.some_value -= 1 }.to change(@instance, :some_value).by(1)
|
339
|
-
end.to fail_with("expected #some_value to have changed by 1, but was changed by -1")
|
340
|
-
end
|
341
|
-
|
342
|
-
it "provides a #description" do
|
343
|
-
expect(change(@instance, :some_value).by(3).description).to eq "change #some_value by 3"
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
describe "expect { ... }.to change { block }.by(expected)" do
|
348
|
-
before(:each) do
|
349
|
-
@instance = SomethingExpected.new
|
350
|
-
@instance.some_value = 5
|
351
|
-
end
|
352
|
-
|
353
|
-
it "passes when attribute is changed by expected amount" do
|
354
|
-
expect { @instance.some_value += 1 }.to change{@instance.some_value}.by(1)
|
355
|
-
end
|
356
|
-
|
357
|
-
it "fails when the attribute is changed by unexpected amount" do
|
358
|
-
expect do
|
359
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by(1)
|
360
|
-
end.to fail_with("expected result to have changed by 1, but was changed by 2")
|
361
|
-
end
|
362
|
-
|
363
|
-
it "fails when the attribute is changed by unexpected amount in the opposite direction" do
|
364
|
-
expect do
|
365
|
-
expect { @instance.some_value -= 1 }.to change{@instance.some_value}.by(1)
|
366
|
-
end.to fail_with("expected result to have changed by 1, but was changed by -1")
|
367
|
-
end
|
368
|
-
|
369
|
-
it "provides a #description" do
|
370
|
-
expect(change { @instance.some_value }.by(3).description).to eq "change result by 3"
|
371
|
-
end
|
372
|
-
end
|
373
|
-
|
374
|
-
describe "expect { ... }.to change(actual, message).by_at_least(expected)" do
|
375
|
-
before(:each) do
|
376
|
-
@instance = SomethingExpected.new
|
377
|
-
@instance.some_value = 5
|
378
|
-
end
|
379
|
-
|
380
|
-
it "passes when attribute is changed by greater than the expected amount" do
|
381
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_least(1)
|
382
|
-
end
|
383
|
-
|
384
|
-
it "passes when attribute is changed by the expected amount" do
|
385
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_least(2)
|
386
|
-
end
|
387
|
-
|
388
|
-
it "fails when the attribute is changed by less than the expected amount" do
|
389
|
-
expect do
|
390
|
-
expect { @instance.some_value += 1 }.to change(@instance, :some_value).by_at_least(2)
|
391
|
-
end.to fail_with("expected #some_value to have changed by at least 2, but was changed by 1")
|
392
|
-
end
|
393
|
-
|
394
|
-
it "provides a #description" do
|
395
|
-
expect(change(@instance, :some_value).by_at_least(3).description).to eq "change #some_value by at least 3"
|
396
|
-
end
|
397
|
-
end
|
398
|
-
|
399
|
-
describe "expect { ... }.to change { block }.by_at_least(expected)" do
|
400
|
-
before(:each) do
|
401
|
-
@instance = SomethingExpected.new
|
402
|
-
@instance.some_value = 5
|
403
|
-
end
|
404
|
-
|
405
|
-
it "passes when attribute is changed by greater than expected amount" do
|
406
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_least(1)
|
407
|
-
end
|
408
|
-
|
409
|
-
it "passes when attribute is changed by the expected amount" do
|
410
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_least(2)
|
411
|
-
end
|
412
|
-
|
413
|
-
it "fails when the attribute is changed by less than the unexpected amount" do
|
414
|
-
expect do
|
415
|
-
expect { @instance.some_value += 1 }.to change{@instance.some_value}.by_at_least(2)
|
416
|
-
end.to fail_with("expected result to have changed by at least 2, but was changed by 1")
|
417
|
-
end
|
418
|
-
|
419
|
-
it "provides a #description" do
|
420
|
-
expect(change { @instance.some_value }.by_at_least(3).description).to eq "change result by at least 3"
|
421
|
-
end
|
422
|
-
end
|
423
|
-
|
424
|
-
|
425
|
-
describe "expect { ... }.to change(actual, message).by_at_most(expected)" do
|
426
|
-
before(:each) do
|
427
|
-
@instance = SomethingExpected.new
|
428
|
-
@instance.some_value = 5
|
429
|
-
end
|
430
|
-
|
431
|
-
it "passes when attribute is changed by less than the expected amount" do
|
432
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(3)
|
433
|
-
end
|
434
|
-
|
435
|
-
it "passes when attribute is changed by the expected amount" do
|
436
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(2)
|
437
|
-
end
|
438
|
-
|
439
|
-
it "fails when the attribute is changed by greater than the expected amount" do
|
440
|
-
expect do
|
441
|
-
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(1)
|
442
|
-
end.to fail_with("expected #some_value to have changed by at most 1, but was changed by 2")
|
443
|
-
end
|
444
|
-
|
445
|
-
it "provides a #description" do
|
446
|
-
expect(change(@instance, :some_value).by_at_most(3).description).to eq "change #some_value by at most 3"
|
447
|
-
end
|
448
|
-
end
|
449
|
-
|
450
|
-
describe "expect { ... }.to change { block }.by_at_most(expected)" do
|
451
|
-
before(:each) do
|
452
|
-
@instance = SomethingExpected.new
|
453
|
-
@instance.some_value = 5
|
454
|
-
end
|
455
|
-
|
456
|
-
it "passes when attribute is changed by less than expected amount" do
|
457
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_most(3)
|
458
|
-
end
|
459
|
-
|
460
|
-
it "passes when attribute is changed by the expected amount" do
|
461
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_most(2)
|
462
|
-
end
|
463
|
-
|
464
|
-
it "fails when the attribute is changed by greater than the unexpected amount" do
|
465
|
-
expect do
|
466
|
-
expect { @instance.some_value += 2 }.to change{@instance.some_value}.by_at_most(1)
|
467
|
-
end.to fail_with("expected result to have changed by at most 1, but was changed by 2")
|
468
|
-
end
|
469
|
-
|
470
|
-
it "provides a #description" do
|
471
|
-
expect(change { @instance.some_value }.by_at_most(3).description).to eq "change result by at most 3"
|
472
|
-
end
|
473
|
-
end
|
474
|
-
|
475
|
-
describe "expect { ... }.to change(actual, message).from(old)" do
|
476
|
-
context "with boolean values" do
|
477
|
-
before(:each) do
|
478
|
-
@instance = SomethingExpected.new
|
479
|
-
@instance.some_value = true
|
480
|
-
end
|
481
|
-
|
482
|
-
it "passes when attribute is == to expected value before executing block" do
|
483
|
-
expect { @instance.some_value = false }.to change(@instance, :some_value).from(true)
|
484
|
-
end
|
485
|
-
|
486
|
-
it "fails when attribute is not == to expected value before executing block" do
|
487
|
-
expect do
|
488
|
-
expect { @instance.some_value = 'foo' }.to change(@instance, :some_value).from(false)
|
489
|
-
end.to fail_with("expected #some_value to have initially been false, but was true")
|
490
|
-
end
|
491
|
-
end
|
492
|
-
context "with non-boolean values" do
|
493
|
-
before(:each) do
|
494
|
-
@instance = SomethingExpected.new
|
495
|
-
@instance.some_value = 'string'
|
496
|
-
end
|
497
|
-
|
498
|
-
it "passes when attribute matches expected value before executing block" do
|
499
|
-
expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from("string")
|
500
|
-
end
|
501
|
-
|
502
|
-
it "fails when attribute does not match expected value before executing block" do
|
503
|
-
expect do
|
504
|
-
expect { @instance.some_value = "knot" }.to change(@instance, :some_value).from("cat")
|
505
|
-
end.to fail_with("expected #some_value to have initially been \"cat\", but was \"string\"")
|
506
|
-
end
|
507
|
-
|
508
|
-
it "provides a #description" do
|
509
|
-
expect(change(@instance, :some_value).from(3).description).to eq "change #some_value from 3"
|
510
|
-
end
|
511
|
-
end
|
512
|
-
end
|
513
|
-
|
514
|
-
describe "expect { ... }.to change { block }.from(old)" do
|
515
|
-
before(:each) do
|
516
|
-
@instance = SomethingExpected.new
|
517
|
-
@instance.some_value = 'string'
|
518
|
-
end
|
519
|
-
|
520
|
-
it "passes when attribute matches expected value before executing block" do
|
521
|
-
expect { @instance.some_value = "astring" }.to change{@instance.some_value}.from("string")
|
522
|
-
end
|
523
|
-
|
524
|
-
it "fails when attribute does not match expected value before executing block" do
|
525
|
-
expect do
|
526
|
-
expect { @instance.some_value = "knot" }.to change{@instance.some_value}.from("cat")
|
527
|
-
end.to fail_with("expected result to have initially been \"cat\", but was \"string\"")
|
528
|
-
end
|
529
|
-
|
530
|
-
it "fails when attribute does not change" do
|
531
|
-
expect do
|
532
|
-
expect { }.to change { @instance.some_value }.from("string")
|
533
|
-
end.to fail_with('expected result to have changed from "string", but did not change')
|
534
|
-
end
|
535
|
-
|
536
|
-
it "provides a #description" do
|
537
|
-
expect(change { }.from(3).description).to eq "change result from 3"
|
538
|
-
end
|
539
|
-
end
|
540
|
-
|
541
|
-
describe "expect { ... }.to change(actual, message).to(new)" do
|
542
|
-
context "with boolean values" do
|
543
|
-
before(:each) do
|
544
|
-
@instance = SomethingExpected.new
|
545
|
-
@instance.some_value = true
|
546
|
-
end
|
547
|
-
|
548
|
-
it "passes when attribute is == to expected value after executing block" do
|
549
|
-
expect { @instance.some_value = false }.to change(@instance, :some_value).to(false)
|
550
|
-
end
|
551
|
-
|
552
|
-
it "fails when attribute is not == to expected value after executing block" do
|
553
|
-
expect do
|
554
|
-
expect { @instance.some_value = 1 }.to change(@instance, :some_value).from(true).to(false)
|
555
|
-
end.to fail_with("expected #some_value to have changed to false, but is now 1")
|
556
|
-
end
|
557
|
-
end
|
558
|
-
|
559
|
-
context "with non-boolean values" do
|
560
|
-
before(:each) do
|
561
|
-
@instance = SomethingExpected.new
|
562
|
-
@instance.some_value = 'string'
|
563
|
-
end
|
564
|
-
|
565
|
-
it "passes when attribute matches expected value after executing block" do
|
566
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat")
|
567
|
-
end
|
568
|
-
|
569
|
-
it "fails when attribute does not match expected value after executing block" do
|
570
|
-
expect do
|
571
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog")
|
572
|
-
end.to fail_with("expected #some_value to have changed to \"dog\", but is now \"cat\"")
|
573
|
-
end
|
574
|
-
|
575
|
-
it "fails with a clear message when it ends with the right value but did not change" do
|
576
|
-
expect {
|
577
|
-
expect { }.to change(@instance, :some_value).to("string")
|
578
|
-
}.to fail_with('expected #some_value to have changed to "string", but did not change')
|
579
|
-
end
|
580
|
-
end
|
581
|
-
end
|
582
|
-
|
583
|
-
describe "expect { ... }.to change { block }.to(new)" do
|
584
|
-
before(:each) do
|
585
|
-
@instance = SomethingExpected.new
|
586
|
-
@instance.some_value = 'string'
|
587
|
-
end
|
588
|
-
|
589
|
-
it "passes when attribute matches expected value after executing block" do
|
590
|
-
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to("cat")
|
591
|
-
end
|
592
|
-
|
593
|
-
it "fails when attribute does not match expected value after executing block" do
|
594
|
-
expect do
|
595
|
-
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.from("string").to("dog")
|
596
|
-
end.to fail_with("expected result to have changed to \"dog\", but is now \"cat\"")
|
597
|
-
end
|
598
|
-
|
599
|
-
it "provides a #description" do
|
600
|
-
expect(change { }.to(3).description).to eq "change result to 3"
|
601
|
-
end
|
602
|
-
end
|
603
|
-
|
604
|
-
describe "expect { ... }.to change(actual, message).from(old).to(new)" do
|
605
|
-
before(:each) do
|
606
|
-
@instance = SomethingExpected.new
|
607
|
-
@instance.some_value = 'string'
|
608
|
-
end
|
609
|
-
|
610
|
-
it "passes when #to comes before #from" do
|
611
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat").from("string")
|
612
|
-
end
|
613
|
-
|
614
|
-
it "passes when #from comes before #to" do
|
615
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("cat")
|
616
|
-
end
|
617
|
-
|
618
|
-
it "shows the correct messaging when #after and #to are different" do
|
619
|
-
expect do
|
620
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog")
|
621
|
-
end.to fail_with("expected #some_value to have changed to \"dog\", but is now \"cat\"")
|
622
|
-
end
|
623
|
-
|
624
|
-
it "shows the correct messaging when #before and #from are different" do
|
625
|
-
expect do
|
626
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("not_string").to("cat")
|
627
|
-
end.to fail_with("expected #some_value to have initially been \"not_string\", but was \"string\"")
|
628
|
-
end
|
629
|
-
end
|
630
|
-
|
631
|
-
describe "expect { ... }.to change { block }.from(old).to(new)" do
|
632
|
-
before(:each) do
|
633
|
-
@instance = SomethingExpected.new
|
634
|
-
@instance.some_value = 'string'
|
635
|
-
end
|
636
|
-
|
637
|
-
context "when #to comes before #from" do
|
638
|
-
it "passes" do
|
639
|
-
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.to("cat").from("string")
|
640
|
-
end
|
641
|
-
|
642
|
-
it "provides a #description" do
|
643
|
-
expect(change { }.to(1).from(3).description).to eq "change result to 1 from 3"
|
644
|
-
end
|
645
|
-
end
|
646
|
-
|
647
|
-
context "when #from comes before #to" do
|
648
|
-
it "passes" do
|
649
|
-
expect { @instance.some_value = "cat" }.to change{@instance.some_value}.from("string").to("cat")
|
650
|
-
end
|
651
|
-
|
652
|
-
it "provides a #description" do
|
653
|
-
expect(change { }.from(1).to(3).description).to eq "change result from 1 to 3"
|
654
|
-
end
|
655
|
-
end
|
656
|
-
end
|
657
|
-
|
658
|
-
describe "Composing a matcher with `change`" do
|
659
|
-
describe "expect { ... }.to change { ... }" do
|
660
|
-
context ".from(matcher).to(matcher)" do
|
661
|
-
it 'passes when the matchers match the from and to values' do
|
662
|
-
k = 0.51
|
663
|
-
expect { k += 1 }.to change { k }.
|
664
|
-
from( a_value_within(0.1).of(0.5) ).
|
665
|
-
to( a_value_within(0.1).of(1.5) )
|
666
|
-
end
|
667
|
-
|
668
|
-
it 'fails with a clear message when the `from` does not match' do
|
669
|
-
expect {
|
670
|
-
k = 0.51
|
671
|
-
expect { k += 1 }.to change { k }.
|
672
|
-
from( a_value_within(0.1).of(0.7) ).
|
673
|
-
to( a_value_within(0.1).of(1.5) )
|
674
|
-
}.to fail_with(/expected result to have initially been a value within 0.1 of 0.7, but was 0.51/)
|
675
|
-
end
|
676
|
-
|
677
|
-
it 'fails with a clear message when the `to` does not match' do
|
678
|
-
expect {
|
679
|
-
k = 0.51
|
680
|
-
expect { k += 1 }.to change { k }.
|
681
|
-
from( a_value_within(0.1).of(0.5) ).
|
682
|
-
to( a_value_within(0.1).of(2.5) )
|
683
|
-
}.to fail_with(/expected result to have changed to a value within 0.1 of 2.5, but is now 1.51/)
|
684
|
-
end
|
685
|
-
|
686
|
-
it 'provides a description' do
|
687
|
-
expect(change(nil, :foo).
|
688
|
-
from( a_value_within(0.1).of(0.5) ).
|
689
|
-
to( a_value_within(0.1).of(1.5) ).description
|
690
|
-
).to eq("change #foo from a value within 0.1 of 0.5 to a value within 0.1 of 1.5")
|
691
|
-
end
|
692
|
-
end
|
693
|
-
|
694
|
-
context ".to(matcher).from(matcher)" do
|
695
|
-
it 'passes when the matchers match the from and to values' do
|
696
|
-
k = 0.51
|
697
|
-
expect { k += 1 }.to change { k }.
|
698
|
-
to( a_value_within(0.1).of(1.5) ).
|
699
|
-
from( a_value_within(0.1).of(0.5) )
|
700
|
-
end
|
701
|
-
|
702
|
-
it 'fails with a clear message when the `from` does not match' do
|
703
|
-
expect {
|
704
|
-
k = 0.51
|
705
|
-
expect { k += 1 }.to change { k }.
|
706
|
-
to( a_value_within(0.1).of(1.5) ).
|
707
|
-
from( a_value_within(0.1).of(0.7) )
|
708
|
-
}.to fail_with(/expected result to have initially been a value within 0.1 of 0.7, but was 0.51/)
|
709
|
-
end
|
710
|
-
|
711
|
-
it 'fails with a clear message when the `to` does not match' do
|
712
|
-
expect {
|
713
|
-
k = 0.51
|
714
|
-
expect { k += 1 }.to change { k }.
|
715
|
-
to( a_value_within(0.1).of(2.5) ).
|
716
|
-
from( a_value_within(0.1).of(0.5) )
|
717
|
-
}.to fail_with(/expected result to have changed to a value within 0.1 of 2.5, but is now 1.51/)
|
718
|
-
end
|
719
|
-
|
720
|
-
it 'provides a description' do
|
721
|
-
expect(change(nil, :foo).
|
722
|
-
to( a_value_within(0.1).of(0.5) ).
|
723
|
-
from( a_value_within(0.1).of(1.5) ).description
|
724
|
-
).to eq("change #foo to a value within 0.1 of 0.5 from a value within 0.1 of 1.5")
|
725
|
-
end
|
726
|
-
end
|
727
|
-
|
728
|
-
context ".by(matcher)" do
|
729
|
-
it "passes when the matcher matches" do
|
730
|
-
k = 0.5
|
731
|
-
expect { k += 1.05 }.to change { k }.by( a_value_within(0.1).of(1) )
|
732
|
-
end
|
733
|
-
|
734
|
-
it 'fails with a clear message when the `by` does not match' do
|
735
|
-
expect {
|
736
|
-
k = 0.5
|
737
|
-
expect { k += 1.05 }.to change { k }.by( a_value_within(0.1).of(0.5) )
|
738
|
-
}.to fail_with(/expected result to have changed by a value within 0.1 of 0.5, but was changed by 1.05/)
|
739
|
-
end
|
740
|
-
|
741
|
-
it 'provides a description' do
|
742
|
-
expect(change(nil, :foo).
|
743
|
-
by( a_value_within(0.1).of(0.5) ).description
|
744
|
-
).to eq("change #foo by a value within 0.1 of 0.5")
|
745
|
-
end
|
746
|
-
end
|
747
|
-
end
|
748
|
-
|
749
|
-
describe "expect { ... }.not_to change { ... }.from(matcher).to(matcher)" do
|
750
|
-
it 'passes when the matcher matches the `from` value and it does not change' do
|
751
|
-
k = 0.51
|
752
|
-
expect { }.not_to change { k }.from( a_value_within(0.1).of(0.5) )
|
753
|
-
end
|
754
|
-
|
755
|
-
it 'fails with a clear message when the `from` matcher does not match' do
|
756
|
-
expect {
|
757
|
-
k = 0.51
|
758
|
-
expect { }.not_to change { k }.from( a_value_within(0.1).of(1.5) )
|
759
|
-
}.to fail_with(/expected result to have initially been a value within 0.1 of 1.5, but was 0.51/)
|
760
|
-
end
|
761
|
-
end
|
762
|
-
end
|
763
|
-
|
764
|
-
describe RSpec::Matchers::BuiltIn::Change do
|
765
|
-
it "works when the receiver has implemented #send" do
|
766
|
-
@instance = SomethingExpected.new
|
767
|
-
@instance.some_value = "string"
|
768
|
-
def @instance.send(*args); raise "DOH! Library developers shouldn't use #send!" end
|
769
|
-
|
770
|
-
expect {
|
771
|
-
expect { @instance.some_value = "cat" }.to change(@instance, :some_value)
|
772
|
-
}.not_to raise_error
|
773
|
-
end
|
774
|
-
|
775
|
-
k = 1
|
776
|
-
before { k = 1 }
|
777
|
-
it_behaves_like "an RSpec matcher", :valid_value => lambda { k += 1 },
|
778
|
-
:invalid_value => lambda { } do
|
779
|
-
let(:matcher) { change { k } }
|
780
|
-
end
|
781
|
-
end
|
782
|
-
|
783
|
-
describe RSpec::Matchers::BuiltIn::ChangeRelatively do
|
784
|
-
k = 0
|
785
|
-
before { k = 0 }
|
786
|
-
it_behaves_like "an RSpec matcher", :valid_value => lambda { k += 1 },
|
787
|
-
:invalid_value => lambda { k += 2 } do
|
788
|
-
let(:matcher) { change { k }.by(1) }
|
789
|
-
end
|
790
|
-
end
|
791
|
-
|
792
|
-
describe RSpec::Matchers::BuiltIn::ChangeFromValue do
|
793
|
-
k = 0
|
794
|
-
before { k = 0 }
|
795
|
-
it_behaves_like "an RSpec matcher", :valid_value => lambda { k += 1 },
|
796
|
-
:invalid_value => lambda { } do
|
797
|
-
let(:matcher) { change { k }.from(0) }
|
798
|
-
end
|
799
|
-
end
|
800
|
-
|
801
|
-
describe RSpec::Matchers::BuiltIn::ChangeToValue do
|
802
|
-
k = 0
|
803
|
-
before { k = 0 }
|
804
|
-
it_behaves_like "an RSpec matcher", :valid_value => lambda { k = 2 },
|
805
|
-
:invalid_value => lambda { k = 3 } do
|
806
|
-
let(:matcher) { change { k }.to(2) }
|
807
|
-
end
|
808
|
-
end
|