Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec should change count without lambda

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 !!

like image 327
athap Avatar asked Sep 06 '12 19:09

athap


2 Answers

subject { lambda { ... } }

it { should change(Model, :count).by(1) }
like image 112
megas Avatar answered Oct 06 '22 15:10

megas


You can also use the expect syntax:

describe "some stuff" do
  expect { ... }.to change(Model, :count).by(1)
end
like image 28
user341493 Avatar answered Oct 06 '22 15:10

user341493