Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for RSpec to expect change in two tables?

Tags:

ruby

rspec

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  
like image 671
B Seven Avatar asked Nov 29 '12 00:11

B Seven


2 Answers

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

like image 103
Georg Ledermann Avatar answered Oct 04 '22 14:10

Georg Ledermann


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.

like image 44
Fred Willmore Avatar answered Oct 04 '22 12:10

Fred Willmore