Rails 4.2.0
悲観的ロックを使用してカウンターを変更する方法があります
class Foo < < ActiveRecord::Base
def bump!
transaction do
lock!
parent.lock!
lock.counter += 1
parent.counter += 1
save!
parent.save!
end
end
end
私はRspec 3.1
それをそのようにテストするために使用しています
expect{foo.bump!}.to change(foo, :counter).by(1)
expect{foo.bump!}.to change(foo.parent, :counter).by(1)
最初のchange(foo, :counter)
テストは成功しますが、両方をコメントアウトしない限り、2 番目のテストは失敗change(foo.parent, :counter)
します。lock!
parent.lock!
失敗したテストをこのように書き直すと、合格します
prev_counter = foo.parent.counter
foo.bump!
expect(foo.parent.counter).to eq prev_counter + 1
で動作しないのはなぜexpect{...}.to change
ですか?