RSpec expect change:
it "should increment the count" do expect{Foo.bar}.to change{Counter.count}.by 1 end
Is there a way to expect change in two tables?
expect{Foo.bar}.to change{Counter.count}.by 1 and change{AnotherCounter.count}.by 1
I prefer this syntax (rspec 3 or later):
it "should increment the counters" do expect { Foo.bar }.to change { Counter, :count }.by(1).and \ change { AnotherCounter, :count }.by(1) end
Yes, this are two assertions in one place, but because the block is executed just one time, it can speedup the tests.
EDIT: Added Backslash after the .and
to avoid syntax error
I got syntax errors trying to use @MichaelJohnston's solution; this is the form that finally worked for me:
it "should increment the counters" do expect { Foo.bar }.to change { Counter.count }.by(1) .and change { AnotherCounter.count }.by(1) end
I should mention I'm using ruby 2.2.2p95 - I don't know if this version has some subtle change in parsing that causes me to get errors, it doesn't appear that anyone else in this thread has had that problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With