I have a Rails 4.2.0
method that uses pessimistic locking to change a counter
class Foo < < ActiveRecord::Base
def bump!
transaction do
lock!
parent.lock!
lock.counter += 1
parent.counter += 1
save!
parent.save!
end
end
end
I am using Rspec 3.1
to test it like so
expect{foo.bump!}.to change(foo, :counter).by(1)
expect{foo.bump!}.to change(foo.parent, :counter).by(1)
The first change(foo, :counter)
test passes but the second change(foo.parent, :counter)
fails unless I comment out both lock!
and parent.lock!
If I rewrite the failing test like this, it passes
prev_counter = foo.parent.counter
foo.bump!
expect(foo.parent.counter).to eq prev_counter + 1
Why doesn't it work with expect{...}.to change
?