Rspec should change count without lambda
Asked Answered
F

2

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

Frostbite answered 6/9, 2012 at 19:7 Comment(0)
F
31
subject { lambda { ... } }

it { should change(Model, :count).by(1) }
Ferrate answered 6/9, 2012 at 19:15 Comment(1)
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?Antilog
P
5

You can also use the expect syntax:

describe "some stuff" do
  expect { ... }.to change(Model, :count).by(1)
end
Pantry answered 16/9, 2013 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.