11

I am trying to figure out another way of writing the should change count test (without lambda). I am using Rails 3. I am also utilizing the shoulda matcher gem

Reason - All test cases are in the format

describe "some stuff" do
   it { should ... }
 end

But I am not able to follow the same pattern for testing the should change count

Here is what I have

describe "some stuff" do
    it "should change count by one" do 
        lambda { ... }.should change(Model, :count).by(1)
    end 
end

Is there a way to write it

describe "some stuff" do
   it { should change(Model, :count).by(1) }
 end

Thanks a lot !!

athap
  • 1,657
  • 1
  • 12
  • 9

2 Answers2

31
subject { lambda { ... } }

it { should change(Model, :count).by(1) }
megas
  • 21,401
  • 12
  • 79
  • 130
  • I was writing specs like this for a while and actually trying to find a more concise way to do this. Is this the best possible option? – firedev Nov 13 '14 at 06:58
5

You can also use the expect syntax:

describe "some stuff" do
  expect { ... }.to change(Model, :count).by(1)
end
user341493
  • 414
  • 1
  • 7
  • 15