| 1 |
class SomethingExpected |
| 2 |
attr_accessor :some_value |
| 3 |
end |
| 4 |
|
| 5 |
value_pattern = /(?:result|`.+?`)/ |
| 6 |
|
| 7 |
RSpec.describe "expect { ... }.to change ..." do |
| 8 |
context "with a numeric value" do |
| 9 |
before(:example) do |
| 10 |
@instance = SomethingExpected.new |
| 11 |
@instance.some_value = 5 |
| 12 |
end |
| 13 |
|
| 14 |
it "passes when actual is modified by the block" do |
| 15 |
expect { @instance.some_value = 6.0 }.to change(@instance, :some_value) |
| 16 |
end |
| 17 |
|
| 18 |
it "fails when actual is not modified by the block" do |
| 19 |
expect do |
| 20 |
expect {}.to change(@instance, :some_value) |
| 21 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed, but is still 5") |
| 22 |
end |
| 23 |
|
| 24 |
it "provides a #description" do |
| 25 |
expect(change(@instance, :some_value).description).to eq "change `SomethingExpected#some_value`" |
| 26 |
end |
| 27 |
end |
| 28 |
|
| 29 |
it "can specify the change of a variable's class" do |
| 30 |
val = nil |
| 31 |
|
| 32 |
expect { |
| 33 |
val = "string" |
| 34 |
}.to change { val.class }.from(NilClass).to(String) |
| 35 |
|
| 36 |
expect { |
| 37 |
expect { |
| 38 |
val = :symbol |
| 39 |
}.to change { val.class }.from(String).to(NilClass) |
| 40 |
}.to fail_with(/but is now Symbol/) |
| 41 |
end |
| 42 |
|
| 43 |
context "with boolean values" do |
| 44 |
before(:example) do |
| 45 |
@instance = SomethingExpected.new |
| 46 |
@instance.some_value = true |
| 47 |
end |
| 48 |
|
| 49 |
it "passes when actual is modified by the block" do |
| 50 |
expect { @instance.some_value = false }.to change(@instance, :some_value) |
| 51 |
end |
| 52 |
|
| 53 |
it "fails when actual is not modified by the block" do |
| 54 |
expect do |
| 55 |
expect {}.to change(@instance, :some_value) |
| 56 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed, but is still true") |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
context "with set values" do |
| 61 |
it "passes when it should" do |
| 62 |
in_sub_process_if_possible do |
| 63 |
require 'set' |
| 64 |
|
| 65 |
set = Set.new([1]) |
| 66 |
expect { |
| 67 |
set << 2 |
| 68 |
}.to change { set }.from([1].to_set).to([2, 1].to_set) |
| 69 |
end |
| 70 |
end |
| 71 |
|
| 72 |
it "fails when it should" do |
| 73 |
in_sub_process_if_possible do |
| 74 |
require 'set' |
| 75 |
|
| 76 |
expect { |
| 77 |
set = Set.new([1]) |
| 78 |
expect { |
| 79 |
set << 2 |
| 80 |
}.to change { set }.from([1].to_set).to([2, 1, 3].to_set) |
| 81 |
}.to fail_with(/expected #{value_pattern} to have changed to #{Regexp.escape([2, 1, 3].to_set.inspect)}, but is now #{Regexp.escape([1, 2].to_set.inspect)}/) |
| 82 |
end |
| 83 |
end |
| 84 |
end |
| 85 |
|
| 86 |
context "with an IO stream" do |
| 87 |
it "fails when the stream does not change" do |
| 88 |
expect { |
| 89 |
k = STDOUT |
| 90 |
expect {}.to change { k } |
| 91 |
}.to fail_with(/expected #{value_pattern} to have changed/) |
| 92 |
end |
| 93 |
end |
| 94 |
|
| 95 |
it 'correctly detects a change that both mutates and replaces an object' do |
| 96 |
obj = Struct.new(:x).new([]) |
| 97 |
|
| 98 |
expect { |
| 99 |
obj.x << 1 |
| 100 |
obj.x = [1] |
| 101 |
}.to change { obj.x } |
| 102 |
end |
| 103 |
|
| 104 |
it 'does not detect changes in an object that updates its hash upon comparison' do |
| 105 |
obj = Class.new do |
| 106 |
def ==(another) |
| 107 |
@hash = rand |
| 108 |
object_id == another.object_id |
| 109 |
end |
| 110 |
def hash |
| 111 |
@hash ||= super |
| 112 |
end |
| 113 |
end.new |
| 114 |
|
| 115 |
expect {}.not_to change { obj } |
| 116 |
end |
| 117 |
|
| 118 |
context "with nil value" do |
| 119 |
before(:example) do |
| 120 |
@instance = SomethingExpected.new |
| 121 |
@instance.some_value = nil |
| 122 |
end |
| 123 |
|
| 124 |
it "passes when actual is modified by the block" do |
| 125 |
expect { @instance.some_value = false }.to change(@instance, :some_value) |
| 126 |
end |
| 127 |
|
| 128 |
it "fails when actual is not modified by the block" do |
| 129 |
expect do |
| 130 |
expect {}.to change(@instance, :some_value) |
| 131 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed, but is still nil") |
| 132 |
end |
| 133 |
end |
| 134 |
|
| 135 |
context "with a deeply nested object graph" do |
| 136 |
it "passes when a leaf is changed" do |
| 137 |
data = [{ :a => [1, 2] }] |
| 138 |
expect { data[0][:a] << 3 }.to change { data } |
| 139 |
end |
| 140 |
|
| 141 |
it 'fails when no part of it is changed' do |
| 142 |
data = [{ :a => [1, 2] }] |
| 143 |
failure_msg = /expected #{value_pattern} to have changed, but is still #{regexp_inspect data}/ |
| 144 |
|
| 145 |
expect { |
| 146 |
expect { data.to_s }.to change { data } |
| 147 |
}.to fail_with(failure_msg) |
| 148 |
end |
| 149 |
|
| 150 |
it "passes when correctly specifying the exact mutation of a leaf" do |
| 151 |
data = [{ :a => [1, 2] }] |
| 152 |
|
| 153 |
expect { data[0][:a] << 3 }.to change { data }. |
| 154 |
from([{ :a => [1, 2] }]). |
| 155 |
to([{ :a => [1, 2, 3] }]) |
| 156 |
end |
| 157 |
|
| 158 |
it "fails when wrongly specifying the `from` value" do |
| 159 |
data = [{ :a => [1, 2] }] |
| 160 |
expected_initial = [{ :a => [1] }] |
| 161 |
failure_msg = /expected #{value_pattern} to have initially been #{regexp_inspect expected_initial}, but was #{regexp_inspect data}/ |
| 162 |
|
| 163 |
expect { |
| 164 |
expect { data[0][:a] << 3 }.to change { data }. |
| 165 |
from(expected_initial). |
| 166 |
to([{ :a => [1, 2, 3] }]) |
| 167 |
}.to fail_with(failure_msg) |
| 168 |
end |
| 169 |
|
| 170 |
it "fails when wrongly specifying the `to` value" do |
| 171 |
data = [{ :a => [1, 2] }] |
| 172 |
expected_final = [{ :a => [1] }] |
| 173 |
failure_msg = /expected #{value_pattern} to have changed to #{regexp_inspect expected_final}, but is now #{regexp_inspect [{ :a => [1, 2, 3] }]}/ |
| 174 |
|
| 175 |
expect { |
| 176 |
expect { data[0][:a] << 3 }.to change { data }. |
| 177 |
from([{ :a => [1, 2] }]). |
| 178 |
to(expected_final) |
| 179 |
}.to fail_with(failure_msg) |
| 180 |
end |
| 181 |
|
| 182 |
def regexp_inspect(object) |
| 183 |
Regexp.escape(object.inspect) |
| 184 |
end |
| 185 |
end |
| 186 |
|
| 187 |
context "with an array" do |
| 188 |
before(:example) do |
| 189 |
@instance = SomethingExpected.new |
| 190 |
@instance.some_value = [] |
| 191 |
end |
| 192 |
|
| 193 |
it "passes when actual is modified by the block" do |
| 194 |
expect { @instance.some_value << 1 }.to change(@instance, :some_value) |
| 195 |
end |
| 196 |
|
| 197 |
it "fails when a predicate on the actual fails" do |
| 198 |
expect do |
| 199 |
expect { @instance.some_value << 1 }.to change { @instance.some_value }.to be_empty |
| 200 |
end.to fail_with(/#{value_pattern} to have changed to/) |
| 201 |
end |
| 202 |
|
| 203 |
it "passes when a predicate on the actual passes" do |
| 204 |
@instance.some_value = [1] |
| 205 |
expect { @instance.some_value.pop }.to change { @instance.some_value }.to be_empty |
| 206 |
end |
| 207 |
|
| 208 |
it "fails when actual is not modified by the block" do |
| 209 |
expect do |
| 210 |
expect {}.to change(@instance, :some_value) |
| 211 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed, but is still []") |
| 212 |
end |
| 213 |
end |
| 214 |
|
| 215 |
context "with a hash" do |
| 216 |
before(:example) do |
| 217 |
@instance = SomethingExpected.new |
| 218 |
@instance.some_value = { :a => 'a' } |
| 219 |
end |
| 220 |
|
| 221 |
it "passes when actual is modified by the block" do |
| 222 |
expect { @instance.some_value[:a] = 'A' }.to change(@instance, :some_value) |
| 223 |
end |
| 224 |
|
| 225 |
it "fails when actual is not modified by the block" do |
| 226 |
expect do |
| 227 |
expect {}.to change(@instance, :some_value) |
| 228 |
end.to fail |
| 229 |
end |
| 230 |
end |
| 231 |
|
| 232 |
context "with a string" do |
| 233 |
it "passes when actual is modified by the block" do |
| 234 |
string = "ab".dup |
| 235 |
expect { string << "c" }.to change { string } |
| 236 |
end |
| 237 |
|
| 238 |
it 'fails when actual is not modified by the block' do |
| 239 |
string = "ab" |
| 240 |
expect { |
| 241 |
expect {}.to change { string } |
| 242 |
}.to fail_with(/to have changed/) |
| 243 |
end |
| 244 |
end |
| 245 |
|
| 246 |
context "with an arbitrary enumerable" do |
| 247 |
before(:example) do |
| 248 |
@instance = SomethingExpected.new |
| 249 |
|
| 250 |
@instance.some_value = Class.new do |
| 251 |
include Enumerable |
| 252 |
|
| 253 |
attr_reader :elements |
| 254 |
|
| 255 |
def initialize(*elements) |
| 256 |
@elements = elements.dup |
| 257 |
end |
| 258 |
|
| 259 |
def <<(element) |
| 260 |
elements << element |
| 261 |
end |
| 262 |
|
| 263 |
def dup |
| 264 |
self.class.new(*elements) |
| 265 |
end |
| 266 |
|
| 267 |
def ==(other) |
| 268 |
elements == other.elements |
| 269 |
end |
| 270 |
|
| 271 |
def hash |
| 272 |
elements.hash |
| 273 |
end |
| 274 |
end.new |
| 275 |
|
| 276 |
end |
| 277 |
|
| 278 |
it "passes when actual is modified by the block" do |
| 279 |
expect { @instance.some_value << 1 }.to change(@instance, :some_value) |
| 280 |
end |
| 281 |
|
| 282 |
it "fails when actual is not modified by the block" do |
| 283 |
expect do |
| 284 |
expect {}.to change(@instance, :some_value) |
| 285 |
end.to fail_with(/^expected `SomethingExpected#some_value` to have changed, but is still/) |
| 286 |
end |
| 287 |
end |
| 288 |
end |
| 289 |
|
| 290 |
RSpec.describe "expect { ... }.to change(actual, message)" do |
| 291 |
it 'provides a #description with `SomeClass#some_message` notation' do |
| 292 |
expect(change('instance', :some_value).description).to eq 'change `String#some_value`' |
| 293 |
end |
| 294 |
|
| 295 |
context "when the receiver is an instance of anonymous class" do |
| 296 |
let(:klass) do |
| 297 |
Class.new(SomethingExpected) |
| 298 |
end |
| 299 |
|
| 300 |
it "can handle it" do |
| 301 |
expect(change(klass.new, :some_value).description).to match(/change `#<Class:.*?>#some_value`/) |
| 302 |
end |
| 303 |
end |
| 304 |
|
| 305 |
context 'when the receiver is an object that does not respond to #class such as BasicObject' do |
| 306 |
let(:basic_object) do |
| 307 |
basic_object_class.new |
| 308 |
end |
| 309 |
|
| 310 |
let(:basic_object_class) do |
| 311 |
defined?(BasicObject) ? BasicObject : fake_basic_object_class |
| 312 |
end |
| 313 |
|
| 314 |
let(:fake_basic_object_class) do |
| 315 |
Class.new do |
| 316 |
def self.to_s |
| 317 |
'BasicObject' |
| 318 |
end |
| 319 |
|
| 320 |
undef class, inspect, respond_to? |
| 321 |
end |
| 322 |
end |
| 323 |
|
| 324 |
it 'can properly extract the class name' do |
| 325 |
expect(change(basic_object, :__id__).description).to eq 'change `BasicObject#__id__`' |
| 326 |
end |
| 327 |
end |
| 328 |
|
| 329 |
context "when the receiver is a Module" do |
| 330 |
it "provides a #description with `SomeModule.some_message` notation" do |
| 331 |
expect(change(SomethingExpected, :some_value).description).to match(/change `SomethingExpected.some_value`/) |
| 332 |
end |
| 333 |
end |
| 334 |
|
| 335 |
context "with a missing message" do |
| 336 |
it "fails with an ArgumentError" do |
| 337 |
expect do |
| 338 |
expect {}.to change(:receiver) |
| 339 |
end.to raise_error(ArgumentError, /^`change` requires either an object and message/) |
| 340 |
end |
| 341 |
end |
| 342 |
end |
| 343 |
|
| 344 |
RSpec.describe "expect { ... }.not_to change(actual, message)" do |
| 345 |
before(:example) do |
| 346 |
@instance = SomethingExpected.new |
| 347 |
@instance.some_value = 5 |
| 348 |
end |
| 349 |
|
| 350 |
it "passes when actual is not modified by the block" do |
| 351 |
expect {}.not_to change(@instance, :some_value) |
| 352 |
end |
| 353 |
|
| 354 |
it "fails when actual is not modified by the block" do |
| 355 |
expect do |
| 356 |
expect { @instance.some_value = 6 }.not_to change(@instance, :some_value) |
| 357 |
end.to fail_with("expected `SomethingExpected#some_value` not to have changed, but did change from 5 to 6") |
| 358 |
end |
| 359 |
end |
| 360 |
|
| 361 |
RSpec.describe "expect { ... }.to change { block }" do |
| 362 |
before(:example) do |
| 363 |
@instance = SomethingExpected.new |
| 364 |
@instance.some_value = 5 |
| 365 |
end |
| 366 |
|
| 367 |
it "passes when actual is modified by the block" do |
| 368 |
expect { @instance.some_value = 6 }.to change { @instance.some_value } |
| 369 |
end |
| 370 |
|
| 371 |
it "fails when actual is not modified by the block" do |
| 372 |
expect do |
| 373 |
expect {}.to change { @instance.some_value } |
| 374 |
end.to fail_with(/expected #{value_pattern} to have changed, but is still 5/) |
| 375 |
end |
| 376 |
|
| 377 |
it "warns if passed a block using do/end instead of {}" do |
| 378 |
expect do |
| 379 |
expect {}.to change do; end |
| 380 |
end.to raise_error(SyntaxError, /Block not received by the `change` matcher/) |
| 381 |
end |
| 382 |
|
| 383 |
context 'in Ripper supported environment', :if => RSpec::Support::RubyFeatures.ripper_supported? do |
| 384 |
context 'when the block body fits into a single line' do |
| 385 |
it "provides a #description with the block snippet" do |
| 386 |
expect(change { @instance.some_value }.description).to eq "change `@instance.some_value`" |
| 387 |
end |
| 388 |
end |
| 389 |
|
| 390 |
context 'when the block body spans multiple lines' do |
| 391 |
before do |
| 392 |
def @instance.reload |
| 393 |
end |
| 394 |
end |
| 395 |
|
| 396 |
let(:matcher) do |
| 397 |
change { |
| 398 |
@instance.reload |
| 399 |
@instance.some_value |
| 400 |
} |
| 401 |
end |
| 402 |
|
| 403 |
it "provides a #description with the block snippet" do |
| 404 |
expect(matcher.description).to eq "change result" |
| 405 |
end |
| 406 |
end |
| 407 |
|
| 408 |
context 'when used with an alias name' do |
| 409 |
alias_matcher :modify, :change |
| 410 |
|
| 411 |
it 'can extract the block snippet' do |
| 412 |
expect(modify { @instance.some_value }.description).to eq "modify `@instance.some_value`" |
| 413 |
end |
| 414 |
end |
| 415 |
end |
| 416 |
|
| 417 |
context 'in Ripper unsupported environment', :unless => RSpec::Support::RubyFeatures.ripper_supported? do |
| 418 |
it "provides a #description without the block snippet" do |
| 419 |
expect(change { @instance.some_value }.description).to eq "change result" |
| 420 |
end |
| 421 |
end |
| 422 |
end |
| 423 |
|
| 424 |
RSpec.describe "expect { ... }.not_to change { block }" do |
| 425 |
before(:example) do |
| 426 |
@instance = SomethingExpected.new |
| 427 |
@instance.some_value = 5 |
| 428 |
end |
| 429 |
|
| 430 |
it "passes when actual is modified by the block" do |
| 431 |
expect {}.not_to change { @instance.some_value } |
| 432 |
end |
| 433 |
|
| 434 |
it "fails when actual is not modified by the block" do |
| 435 |
expect do |
| 436 |
expect { @instance.some_value = 6 }.not_to change { @instance.some_value } |
| 437 |
end.to fail_with(/expected #{value_pattern} not to have changed, but did change from 5 to 6/) |
| 438 |
end |
| 439 |
|
| 440 |
it "warns if passed a block using do/end instead of {}" do |
| 441 |
expect do |
| 442 |
expect {}.not_to change do; end |
| 443 |
end.to raise_error(SyntaxError, /Block not received by the `change` matcher/) |
| 444 |
end |
| 445 |
|
| 446 |
context "with an IO stream" do |
| 447 |
it "passes when the stream does not change" do |
| 448 |
k = STDOUT |
| 449 |
expect {}.not_to change { k } |
| 450 |
end |
| 451 |
end |
| 452 |
|
| 453 |
context "with a deeply nested object graph" do |
| 454 |
it "passes when the object is changed" do |
| 455 |
data = [{ :a => [1, 2] }] |
| 456 |
expect { data.to_s }.not_to change { data } |
| 457 |
end |
| 458 |
|
| 459 |
it 'fails when part of it is changed' do |
| 460 |
data = [{ :a => [1, 2] }] |
| 461 |
failure_msg = /expected #{value_pattern} not to have changed, but did change from #{regexp_inspect data} to #{regexp_inspect [{ :a=>[1, 2, 3] }]}/ |
| 462 |
|
| 463 |
expect { |
| 464 |
expect { data[0][:a] << 3 }.not_to change { data } |
| 465 |
}.to fail_with(failure_msg) |
| 466 |
end |
| 467 |
|
| 468 |
it "passes when correctly specifying the exact mutation of a leaf" do |
| 469 |
data = [{ :a => [1, 2] }] |
| 470 |
|
| 471 |
expect { data[0][:a] << 3 }.to change { data }. |
| 472 |
from([{ :a => [1, 2] }]). |
| 473 |
to([{ :a => [1, 2, 3] }]) |
| 474 |
end |
| 475 |
|
| 476 |
def regexp_inspect(object) |
| 477 |
Regexp.escape(object.inspect) |
| 478 |
end |
| 479 |
end |
| 480 |
end |
| 481 |
|
| 482 |
RSpec.describe "expect { ... }.not_to change { }.from" do |
| 483 |
context 'when the value starts at the from value' do |
| 484 |
it 'passes when the value does not change' do |
| 485 |
k = 5 |
| 486 |
expect {}.not_to change { k }.from(5) |
| 487 |
end |
| 488 |
|
| 489 |
it 'fails when the value does change' do |
| 490 |
expect { |
| 491 |
k = 5 |
| 492 |
expect { k += 1 }.not_to change { k }.from(5) |
| 493 |
}.to fail_with(/but did change from 5 to 6/) |
| 494 |
end |
| 495 |
end |
| 496 |
|
| 497 |
context 'when the value starts at a different value' do |
| 498 |
it 'fails when the value does not change' do |
| 499 |
expect { |
| 500 |
k = 6 |
| 501 |
expect {}.not_to change { k }.from(5) |
| 502 |
}.to fail_with(/expected #{value_pattern} to have initially been 5/) |
| 503 |
end |
| 504 |
|
| 505 |
it 'fails when the value does change' do |
| 506 |
expect { |
| 507 |
k = 6 |
| 508 |
expect { k += 1 }.not_to change { k }.from(5) |
| 509 |
}.to fail_with(/expected #{value_pattern} to have initially been 5/) |
| 510 |
end |
| 511 |
end |
| 512 |
end |
| 513 |
|
| 514 |
RSpec.describe "expect { ... }.not_to change { }.to" do |
| 515 |
it 'is not supported' do |
| 516 |
expect { |
| 517 |
expect {}.not_to change {}.to(3) |
| 518 |
}.to raise_error(NotImplementedError) |
| 519 |
end |
| 520 |
|
| 521 |
it 'is not supported when it comes after `from`' do |
| 522 |
expect { |
| 523 |
expect {}.not_to change {}.from(nil).to(3) |
| 524 |
}.to raise_error(NotImplementedError) |
| 525 |
end |
| 526 |
end |
| 527 |
|
| 528 |
RSpec.describe "expect { ... }.not_to change { }.by" do |
| 529 |
it 'is not supported' do |
| 530 |
expect { |
| 531 |
expect {}.not_to change {}.by(3) |
| 532 |
}.to raise_error(NotImplementedError) |
| 533 |
end |
| 534 |
end |
| 535 |
|
| 536 |
RSpec.describe "expect { ... }.not_to change { }.by_at_least" do |
| 537 |
it 'is not supported' do |
| 538 |
expect { |
| 539 |
expect {}.not_to change {}.by_at_least(3) |
| 540 |
}.to raise_error(NotImplementedError) |
| 541 |
end |
| 542 |
end |
| 543 |
|
| 544 |
RSpec.describe "expect { ... }.not_to change { }.by_at_most" do |
| 545 |
it 'is not supported' do |
| 546 |
expect { |
| 547 |
expect {}.not_to change {}.by_at_most(3) |
| 548 |
}.to raise_error(NotImplementedError) |
| 549 |
end |
| 550 |
end |
| 551 |
|
| 552 |
RSpec.describe "expect { ... }.to change(actual, message).by(expected)" do |
| 553 |
before(:example) do |
| 554 |
@instance = SomethingExpected.new |
| 555 |
@instance.some_value = 5 |
| 556 |
end |
| 557 |
|
| 558 |
it "passes when attribute is changed by expected amount" do |
| 559 |
expect { @instance.some_value += 1 }.to change(@instance, :some_value).by(1) |
| 560 |
end |
| 561 |
|
| 562 |
it "passes when attribute is not changed and expected amount is 0" do |
| 563 |
expect { @instance.some_value += 0 }.to change(@instance, :some_value).by(0) |
| 564 |
end |
| 565 |
|
| 566 |
it "fails when the attribute is changed by unexpected amount" do |
| 567 |
expect do |
| 568 |
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by(1) |
| 569 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed by 1, but was changed by 2") |
| 570 |
end |
| 571 |
|
| 572 |
it "fails when the attribute is changed by unexpected amount in the opposite direction" do |
| 573 |
expect do |
| 574 |
expect { @instance.some_value -= 1 }.to change(@instance, :some_value).by(1) |
| 575 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed by 1, but was changed by -1") |
| 576 |
end |
| 577 |
|
| 578 |
it "provides a #description" do |
| 579 |
expect(change(@instance, :some_value).by(3).description).to eq "change `SomethingExpected#some_value` by 3" |
| 580 |
end |
| 581 |
end |
| 582 |
|
| 583 |
RSpec.describe "expect { ... }.to change { block }.by(expected)" do |
| 584 |
before(:example) do |
| 585 |
@instance = SomethingExpected.new |
| 586 |
@instance.some_value = 5 |
| 587 |
end |
| 588 |
|
| 589 |
it "passes when attribute is changed by expected amount" do |
| 590 |
expect { @instance.some_value += 1 }.to change { @instance.some_value }.by(1) |
| 591 |
end |
| 592 |
|
| 593 |
it "fails when the attribute is changed by unexpected amount" do |
| 594 |
expect do |
| 595 |
expect { @instance.some_value += 2 }.to change { @instance.some_value }.by(1) |
| 596 |
end.to fail_with(/expected #{value_pattern} to have changed by 1, but was changed by 2/) |
| 597 |
end |
| 598 |
|
| 599 |
it "fails when the attribute is changed by unexpected amount in the opposite direction" do |
| 600 |
expect do |
| 601 |
expect { @instance.some_value -= 1 }.to change { @instance.some_value }.by(1) |
| 602 |
end.to fail_with(/expected #{value_pattern} to have changed by 1, but was changed by -1/) |
| 603 |
end |
| 604 |
|
| 605 |
context 'in Ripper supported environment', :if => RSpec::Support::RubyFeatures.ripper_supported? do |
| 606 |
it "provides a #description with the block snippet" do |
| 607 |
expect(change { @instance.some_value }.by(3).description).to eq "change `@instance.some_value` by 3" |
| 608 |
end |
| 609 |
end |
| 610 |
|
| 611 |
context 'in Ripper unsupported environment', :unless => RSpec::Support::RubyFeatures.ripper_supported? do |
| 612 |
it "provides a #description without the block snippet" do |
| 613 |
expect(change { @instance.some_value }.by(3).description).to eq "change result by 3" |
| 614 |
end |
| 615 |
end |
| 616 |
end |
| 617 |
|
| 618 |
RSpec.describe "expect { ... }.to change(actual, message).by_at_least(expected)" do |
| 619 |
before(:example) do |
| 620 |
@instance = SomethingExpected.new |
| 621 |
@instance.some_value = 5 |
| 622 |
end |
| 623 |
|
| 624 |
it "passes when attribute is changed by greater than the expected amount" do |
| 625 |
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_least(1) |
| 626 |
end |
| 627 |
|
| 628 |
it "passes when attribute is changed by the expected amount" do |
| 629 |
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_least(2) |
| 630 |
end |
| 631 |
|
| 632 |
it "fails when the attribute is changed by less than the expected amount" do |
| 633 |
expect do |
| 634 |
expect { @instance.some_value += 1 }.to change(@instance, :some_value).by_at_least(2) |
| 635 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed by at least 2, but was changed by 1") |
| 636 |
end |
| 637 |
|
| 638 |
it "provides a #description" do |
| 639 |
expect(change(@instance, :some_value).by_at_least(3).description).to eq "change `SomethingExpected#some_value` by at least 3" |
| 640 |
end |
| 641 |
end |
| 642 |
|
| 643 |
RSpec.describe "expect { ... }.to change { block }.by_at_least(expected)" do |
| 644 |
before(:example) do |
| 645 |
@instance = SomethingExpected.new |
| 646 |
@instance.some_value = 5 |
| 647 |
end |
| 648 |
|
| 649 |
it "passes when attribute is changed by greater than expected amount" do |
| 650 |
expect { @instance.some_value += 2 }.to change { @instance.some_value }.by_at_least(1) |
| 651 |
end |
| 652 |
|
| 653 |
it "passes when attribute is changed by the expected amount" do |
| 654 |
expect { @instance.some_value += 2 }.to change { @instance.some_value }.by_at_least(2) |
| 655 |
end |
| 656 |
|
| 657 |
it "fails when the attribute is changed by less than the unexpected amount" do |
| 658 |
expect do |
| 659 |
expect { @instance.some_value += 1 }.to change { @instance.some_value }.by_at_least(2) |
| 660 |
end.to fail_with(/expected #{value_pattern} to have changed by at least 2, but was changed by 1/) |
| 661 |
end |
| 662 |
|
| 663 |
context 'in Ripper supported environment', :if => RSpec::Support::RubyFeatures.ripper_supported? do |
| 664 |
it "provides a #description with the block snippet" do |
| 665 |
expect(change { @instance.some_value }.by_at_least(3).description).to eq "change `@instance.some_value` by at least 3" |
| 666 |
end |
| 667 |
end |
| 668 |
|
| 669 |
context 'in Ripper unsupported environment', :unless => RSpec::Support::RubyFeatures.ripper_supported? do |
| 670 |
it "provides a #description without the block snippet" do |
| 671 |
expect(change { @instance.some_value }.by_at_least(3).description).to eq "change result by at least 3" |
| 672 |
end |
| 673 |
end |
| 674 |
end |
| 675 |
|
| 676 |
RSpec.describe "expect { ... }.to change(actual, message).by_at_most(expected)" do |
| 677 |
before(:example) do |
| 678 |
@instance = SomethingExpected.new |
| 679 |
@instance.some_value = 5 |
| 680 |
end |
| 681 |
|
| 682 |
it "passes when attribute is changed by less than the expected amount" do |
| 683 |
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(3) |
| 684 |
end |
| 685 |
|
| 686 |
it "passes when attribute is changed by the expected amount" do |
| 687 |
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(2) |
| 688 |
end |
| 689 |
|
| 690 |
it "fails when the attribute is changed by greater than the expected amount" do |
| 691 |
expect do |
| 692 |
expect { @instance.some_value += 2 }.to change(@instance, :some_value).by_at_most(1) |
| 693 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed by at most 1, but was changed by 2") |
| 694 |
end |
| 695 |
|
| 696 |
it "provides a #description" do |
| 697 |
expect(change(@instance, :some_value).by_at_most(3).description).to eq "change `SomethingExpected#some_value` by at most 3" |
| 698 |
end |
| 699 |
end |
| 700 |
|
| 701 |
RSpec.describe "expect { ... }.to change { block }.by_at_most(expected)" do |
| 702 |
before(:example) do |
| 703 |
@instance = SomethingExpected.new |
| 704 |
@instance.some_value = 5 |
| 705 |
end |
| 706 |
|
| 707 |
it "passes when attribute is changed by less than expected amount" do |
| 708 |
expect { @instance.some_value += 2 }.to change { @instance.some_value }.by_at_most(3) |
| 709 |
end |
| 710 |
|
| 711 |
it "passes when attribute is changed by the expected amount" do |
| 712 |
expect { @instance.some_value += 2 }.to change { @instance.some_value }.by_at_most(2) |
| 713 |
end |
| 714 |
|
| 715 |
it "fails when the attribute is changed by greater than the unexpected amount" do |
| 716 |
expect do |
| 717 |
expect { @instance.some_value += 2 }.to change { @instance.some_value }.by_at_most(1) |
| 718 |
end.to fail_with(/expected #{value_pattern} to have changed by at most 1, but was changed by 2/) |
| 719 |
end |
| 720 |
|
| 721 |
context 'in Ripper supported environment', :if => RSpec::Support::RubyFeatures.ripper_supported? do |
| 722 |
it "provides a #description with the block snippet" do |
| 723 |
expect(change { @instance.some_value }.by_at_most(3).description).to eq "change `@instance.some_value` by at most 3" |
| 724 |
end |
| 725 |
end |
| 726 |
|
| 727 |
context 'in Ripper unsupported environment', :unless => RSpec::Support::RubyFeatures.ripper_supported? do |
| 728 |
it "provides a #description without the block snippet" do |
| 729 |
expect(change { @instance.some_value }.by_at_most(3).description).to eq "change result by at most 3" |
| 730 |
end |
| 731 |
end |
| 732 |
end |
| 733 |
|
| 734 |
RSpec.describe "expect { ... }.to change(actual, message).from(old)" do |
| 735 |
context "with boolean values" do |
| 736 |
before(:example) do |
| 737 |
@instance = SomethingExpected.new |
| 738 |
@instance.some_value = true |
| 739 |
end |
| 740 |
|
| 741 |
it "passes when attribute is == to expected value before executing block" do |
| 742 |
expect { @instance.some_value = false }.to change(@instance, :some_value).from(true) |
| 743 |
end |
| 744 |
|
| 745 |
it "fails when attribute is not == to expected value before executing block" do |
| 746 |
expect do |
| 747 |
expect { @instance.some_value = 'foo' }.to change(@instance, :some_value).from(false) |
| 748 |
end.to fail_with("expected `SomethingExpected#some_value` to have initially been false, but was true") |
| 749 |
end |
| 750 |
end |
| 751 |
|
| 752 |
context "with non-boolean values" do |
| 753 |
before(:example) do |
| 754 |
@instance = SomethingExpected.new |
| 755 |
@instance.some_value = 'string' |
| 756 |
end |
| 757 |
|
| 758 |
it "passes when attribute matches expected value before executing block" do |
| 759 |
expect { @instance.some_value = "astring" }.to change(@instance, :some_value).from("string") |
| 760 |
end |
| 761 |
|
| 762 |
it "fails when attribute does not match expected value before executing block" do |
| 763 |
expect do |
| 764 |
expect { @instance.some_value = "knot" }.to change(@instance, :some_value).from("cat") |
| 765 |
end.to fail_with("expected `SomethingExpected#some_value` to have initially been \"cat\", but was \"string\"") |
| 766 |
end |
| 767 |
|
| 768 |
it "provides a #description" do |
| 769 |
expect(change(@instance, :some_value).from(3).description).to eq "change `SomethingExpected#some_value` from 3" |
| 770 |
end |
| 771 |
end |
| 772 |
end |
| 773 |
|
| 774 |
RSpec.describe "expect { ... }.to change { block }.from(old)" do |
| 775 |
before(:example) do |
| 776 |
@instance = SomethingExpected.new |
| 777 |
@instance.some_value = 'string' |
| 778 |
end |
| 779 |
|
| 780 |
it "passes when attribute matches expected value before executing block" do |
| 781 |
expect { @instance.some_value = "astring" }.to change { @instance.some_value }.from("string") |
| 782 |
end |
| 783 |
|
| 784 |
it "fails when attribute does not match expected value before executing block" do |
| 785 |
expect do |
| 786 |
expect { @instance.some_value = "knot" }.to change { @instance.some_value }.from("cat") |
| 787 |
end.to fail_with(/expected #{value_pattern} to have initially been "cat", but was "string"/) |
| 788 |
end |
| 789 |
|
| 790 |
it "fails when attribute does not change" do |
| 791 |
expect do |
| 792 |
expect {}.to change { @instance.some_value }.from("string") |
| 793 |
end.to fail_with(/expected #{value_pattern} to have changed from "string", but did not change/) |
| 794 |
end |
| 795 |
|
| 796 |
context 'in Ripper supported environment', :if => RSpec::Support::RubyFeatures.ripper_supported? do |
| 797 |
it "provides a #description with the block snippet" do |
| 798 |
expect(change { @instance.some_value }.from(3).description).to eq "change `@instance.some_value` from 3" |
| 799 |
end |
| 800 |
end |
| 801 |
|
| 802 |
context 'in Ripper unsupported environment', :unless => RSpec::Support::RubyFeatures.ripper_supported? do |
| 803 |
it "provides a #description without the block snippet" do |
| 804 |
expect(change { @instance.some_value }.from(3).description).to eq "change result from 3" |
| 805 |
end |
| 806 |
end |
| 807 |
|
| 808 |
it "provides a #description" do |
| 809 |
expect(change {}.from(3).description).to eq "change result from 3" |
| 810 |
end |
| 811 |
end |
| 812 |
|
| 813 |
RSpec.describe "expect { ... }.to change(actual, message).to(new)" do |
| 814 |
context "with boolean values" do |
| 815 |
before(:example) do |
| 816 |
@instance = SomethingExpected.new |
| 817 |
@instance.some_value = true |
| 818 |
end |
| 819 |
|
| 820 |
it "passes when attribute is == to expected value after executing block" do |
| 821 |
expect { @instance.some_value = false }.to change(@instance, :some_value).to(false) |
| 822 |
end |
| 823 |
|
| 824 |
it "fails when attribute is not == to expected value after executing block" do |
| 825 |
expect do |
| 826 |
expect { @instance.some_value = 1 }.to change(@instance, :some_value).from(true).to(false) |
| 827 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed to false, but is now 1") |
| 828 |
end |
| 829 |
end |
| 830 |
|
| 831 |
context "with non-boolean values" do |
| 832 |
before(:example) do |
| 833 |
@instance = SomethingExpected.new |
| 834 |
@instance.some_value = 'string' |
| 835 |
end |
| 836 |
|
| 837 |
it "passes when attribute matches expected value after executing block" do |
| 838 |
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat") |
| 839 |
end |
| 840 |
|
| 841 |
it "fails when attribute does not match expected value after executing block" do |
| 842 |
expect do |
| 843 |
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog") |
| 844 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed to \"dog\", but is now \"cat\"") |
| 845 |
end |
| 846 |
|
| 847 |
it "fails with a clear message when it ends with the right value but did not change" do |
| 848 |
expect { |
| 849 |
expect {}.to change(@instance, :some_value).to("string") |
| 850 |
}.to fail_with('expected `SomethingExpected#some_value` to have changed to "string", but did not change') |
| 851 |
end |
| 852 |
end |
| 853 |
end |
| 854 |
|
| 855 |
RSpec.describe "expect { ... }.to change { block }.to(new)" do |
| 856 |
before(:example) do |
| 857 |
@instance = SomethingExpected.new |
| 858 |
@instance.some_value = 'string' |
| 859 |
end |
| 860 |
|
| 861 |
it "passes when attribute matches expected value after executing block" do |
| 862 |
expect { @instance.some_value = "cat" }.to change { @instance.some_value }.to("cat") |
| 863 |
end |
| 864 |
|
| 865 |
it "fails when attribute does not match expected value after executing block" do |
| 866 |
expect do |
| 867 |
expect { @instance.some_value = "cat" }.to change { @instance.some_value }.from("string").to("dog") |
| 868 |
end.to fail_with(/expected #{value_pattern} to have changed to "dog", but is now "cat"/) |
| 869 |
end |
| 870 |
|
| 871 |
it "provides a #description" do |
| 872 |
expect(change {}.to(3).description).to eq "change result to 3" |
| 873 |
end |
| 874 |
end |
| 875 |
|
| 876 |
RSpec.describe "expect { ... }.to change(actual, message).from(old).to(new)" do |
| 877 |
before(:example) do |
| 878 |
@instance = SomethingExpected.new |
| 879 |
@instance.some_value = 'string' |
| 880 |
end |
| 881 |
|
| 882 |
it "passes when #to comes before #from" do |
| 883 |
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).to("cat").from("string") |
| 884 |
end |
| 885 |
|
| 886 |
it "passes when #from comes before #to" do |
| 887 |
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("cat") |
| 888 |
end |
| 889 |
|
| 890 |
it "shows the correct messaging when #after and #to are different" do |
| 891 |
expect do |
| 892 |
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("string").to("dog") |
| 893 |
end.to fail_with("expected `SomethingExpected#some_value` to have changed to \"dog\", but is now \"cat\"") |
| 894 |
end |
| 895 |
|
| 896 |
it "shows the correct messaging when #before and #from are different" do |
| 897 |
expect do |
| 898 |
expect { @instance.some_value = "cat" }.to change(@instance, :some_value).from("not_string").to("cat") |
| 899 |
end.to fail_with("expected `SomethingExpected#some_value` to have initially been \"not_string\", but was \"string\"") |
| 900 |
end |
| 901 |
end |
| 902 |
|
| 903 |
RSpec.describe "expect { ... }.to change { block }.from(old).to(new)" do |
| 904 |
before(:example) do |
| 905 |
@instance = SomethingExpected.new |
| 906 |
@instance.some_value = 'string' |
| 907 |
end |
| 908 |
|
| 909 |
context "when #to comes before #from" do |
| 910 |
it "passes" do |
| 911 |
expect { @instance.some_value = "cat" }.to change { @instance.some_value }.to("cat").from("string") |
| 912 |
end |
| 913 |
|
| 914 |
it "provides a #description" do |
| 915 |
expect(change {}.to(1).from(3).description).to eq "change result to 1 from 3" |
| 916 |
end |
| 917 |
end |
| 918 |
|
| 919 |
context "when #from comes before #to" do |
| 920 |
it "passes" do |
| 921 |
expect { @instance.some_value = "cat" }.to change { @instance.some_value }.from("string").to("cat") |
| 922 |
end |
| 923 |
|
| 924 |
it "provides a #description" do |
| 925 |
expect(change {}.from(1).to(3).description).to eq "change result from 1 to 3" |
| 926 |
end |
| 927 |
end |
| 928 |
end |
| 929 |
|
| 930 |
RSpec.describe "Composing a matcher with `change`" do |
| 931 |
describe "expect { ... }.to change { ... }" do |
| 932 |
context ".from(matcher).to(matcher)" do |
| 933 |
it 'passes when the matchers match the from and to values' do |
| 934 |
k = 0.51 |
| 935 |
expect { k += 1 }.to change { k }. |
| 936 |
from( a_value_within(0.1).of(0.5) ). |
| 937 |
to( a_value_within(0.1).of(1.5) ) |
| 938 |
end |
| 939 |
|
| 940 |
it 'fails with a clear message when the `from` does not match' do |
| 941 |
expect { |
| 942 |
k = 0.51 |
| 943 |
expect { k += 1 }.to change { k }. |
| 944 |
from( a_value_within(0.1).of(0.7) ). |
| 945 |
to( a_value_within(0.1).of(1.5) ) |
| 946 |
}.to fail_with(/expected #{value_pattern} to have initially been a value within 0.1 of 0.7, but was 0.51/) |
| 947 |
end |
| 948 |
|
| 949 |
it 'fails with a clear message when the `to` does not match' do |
| 950 |
expect { |
| 951 |
k = 0.51 |
| 952 |
expect { k += 1 }.to change { k }. |
| 953 |
from( a_value_within(0.1).of(0.5) ). |
| 954 |
to( a_value_within(0.1).of(2.5) ) |
| 955 |
}.to fail_with(/expected #{value_pattern} to have changed to a value within 0.1 of 2.5, but is now 1.51/) |
| 956 |
end |
| 957 |
|
| 958 |
it 'provides a description' do |
| 959 |
expect(change(nil, :foo). |
| 960 |
from( a_value_within(0.1).of(0.5) ). |
| 961 |
to( a_value_within(0.1).of(1.5) ).description |
| 962 |
).to eq("change `NilClass#foo` from a value within 0.1 of 0.5 to a value within 0.1 of 1.5") |
| 963 |
end |
| 964 |
end |
| 965 |
|
| 966 |
context ".to(matcher).from(matcher)" do |
| 967 |
it 'passes when the matchers match the from and to values' do |
| 968 |
k = 0.51 |
| 969 |
expect { k += 1 }.to change { k }. |
| 970 |
to( a_value_within(0.1).of(1.5) ). |
| 971 |
from( a_value_within(0.1).of(0.5) ) |
| 972 |
end |
| 973 |
|
| 974 |
it 'fails with a clear message when the `from` does not match' do |
| 975 |
expect { |
| 976 |
k = 0.51 |
| 977 |
expect { k += 1 }.to change { k }. |
| 978 |
to( a_value_within(0.1).of(1.5) ). |
| 979 |
from( a_value_within(0.1).of(0.7) ) |
| 980 |
}.to fail_with(/expected #{value_pattern} to have initially been a value within 0.1 of 0.7, but was 0.51/) |
| 981 |
end |
| 982 |
|
| 983 |
it 'fails with a clear message when the `to` does not match' do |
| 984 |
expect { |
| 985 |
k = 0.51 |
| 986 |
expect { k += 1 }.to change { k }. |
| 987 |
to( a_value_within(0.1).of(2.5) ). |
| 988 |
from( a_value_within(0.1).of(0.5) ) |
| 989 |
}.to fail_with(/expected #{value_pattern} to have changed to a value within 0.1 of 2.5, but is now 1.51/) |
| 990 |
end |
| 991 |
|
| 992 |
it 'provides a description' do |
| 993 |
expect(change(nil, :foo). |
| 994 |
to( a_value_within(0.1).of(0.5) ). |
| 995 |
from( a_value_within(0.1).of(1.5) ).description |
| 996 |
).to eq("change `NilClass#foo` to a value within 0.1 of 0.5 from a value within 0.1 of 1.5") |
| 997 |
end |
| 998 |
end |
| 999 |
|
| 1000 |
context ".by(matcher)" do |
| 1001 |
it "passes when the matcher matches" do |
| 1002 |
k = 0.5 |
| 1003 |
expect { k += 1.05 }.to change { k }.by( a_value_within(0.1).of(1) ) |
| 1004 |
end |
| 1005 |
|
| 1006 |
it 'fails with a clear message when the `by` does not match' do |
| 1007 |
expect { |
| 1008 |
k = 0.5 |
| 1009 |
expect { k += 1.05 }.to change { k }.by( a_value_within(0.1).of(0.5) ) |
| 1010 |
}.to fail_with(/expected #{value_pattern} to have changed by a value within 0.1 of 0.5, but was changed by 1.05/) |
| 1011 |
end |
| 1012 |
|
| 1013 |
it 'provides a description' do |
| 1014 |
expect(change(nil, :foo). |
| 1015 |
by( a_value_within(0.1).of(0.5) ).description |
| 1016 |
).to eq("change `NilClass#foo` by a value within 0.1 of 0.5") |
| 1017 |
end |
| 1018 |
end |
| 1019 |
end |
| 1020 |
|
| 1021 |
describe "expect { ... }.not_to change { ... }.from(matcher).to(matcher)" do |
| 1022 |
it 'passes when the matcher matches the `from` value and it does not change' do |
| 1023 |
k = 0.51 |
| 1024 |
expect {}.not_to change { k }.from( a_value_within(0.1).of(0.5) ) |
| 1025 |
end |
| 1026 |
|
| 1027 |
it 'fails with a clear message when the `from` matcher does not match' do |
| 1028 |
expect { |
| 1029 |
k = 0.51 |
| 1030 |
expect {}.not_to change { k }.from( a_value_within(0.1).of(1.5) ) |
| 1031 |
}.to fail_with(/expected #{value_pattern} to have initially been a value within 0.1 of 1.5, but was 0.51/) |
| 1032 |
end |
| 1033 |
end |
| 1034 |
end |
| 1035 |
|
| 1036 |
RSpec.describe RSpec::Matchers::BuiltIn::Change do |
| 1037 |
it "works when the receiver has implemented #send" do |
| 1038 |
@instance = SomethingExpected.new |
| 1039 |
@instance.some_value = "string" |
| 1040 |
def @instance.send(*_args); raise "DOH! Library developers shouldn't use #send!" end |
| 1041 |
|
| 1042 |
expect { |
| 1043 |
expect { @instance.some_value = "cat" }.to change(@instance, :some_value) |
| 1044 |
}.not_to raise_error |
| 1045 |
end |
| 1046 |
|
| 1047 |
it_behaves_like "an RSpec block-only matcher" do |
| 1048 |
let(:matcher) { change { @k } } |
| 1049 |
before { @k = 1 } |
| 1050 |
def valid_block |
| 1051 |
@k += 1 |
| 1052 |
end |
| 1053 |
def invalid_block |
| 1054 |
end |
| 1055 |
end |
| 1056 |
end |
| 1057 |
|
| 1058 |
RSpec.describe RSpec::Matchers::BuiltIn::ChangeRelatively do |
| 1059 |
it_behaves_like "an RSpec block-only matcher", :disallows_negation => true, :skip_deprecation_check => true do |
| 1060 |
let(:matcher) { change { @k }.by(1) } |
| 1061 |
before { @k = 0 } |
| 1062 |
def valid_block |
| 1063 |
@k += 1 |
| 1064 |
end |
| 1065 |
def invalid_block |
| 1066 |
@k += 2 |
| 1067 |
end |
| 1068 |
end |
| 1069 |
end |
| 1070 |
|
| 1071 |
RSpec.describe RSpec::Matchers::BuiltIn::ChangeFromValue do |
| 1072 |
it_behaves_like "an RSpec block-only matcher" do |
| 1073 |
let(:matcher) { change { @k }.from(0) } |
| 1074 |
before { @k = 0 } |
| 1075 |
def valid_block |
| 1076 |
@k += 1 |
| 1077 |
end |
| 1078 |
def invalid_block |
| 1079 |
end |
| 1080 |
end |
| 1081 |
end |
| 1082 |
|
| 1083 |
RSpec.describe RSpec::Matchers::BuiltIn::ChangeToValue do |
| 1084 |
it_behaves_like "an RSpec block-only matcher", :disallows_negation => true do |
| 1085 |
let(:matcher) { change { @k }.to(2) } |
| 1086 |
before { @k = 0 } |
| 1087 |
def valid_block |
| 1088 |
@k = 2 |
| 1089 |
end |
| 1090 |
def invalid_block |
| 1091 |
@k = 3 |
| 1092 |
end |
| 1093 |
end |
| 1094 |
end |