Rspec expects me to not do what I expect

So, I have two specifications that I thought test the same thing, but one fails and the other. I am working on an application that has a repeating schedule. If the user creates a repeat trip, he will continue to work and create new trips for each specified day. Here is the first test that fails:

it "makes future trips" do expect{FactoryGirl.create(:recurring_transportation_trip)}.to change(Trip, :count).by(4) end 

The recurring_transportation_trip function creates a trip that will make three future trips through the after_save callback. This test fails with the error "the score should have been changed to 4, but was changed to 1".

Here is another test that passes:

 it "makes future trips" do count = Trip.count FactoryGirl.create(:recurring_transportation_trip) Trip.count == count + 4 end 

It is shown that there is the correct functionality.

The first test is certainly more readable, but doesn’t really check what I think it does. Can anyone suggest and explain why?

------- EDIT -------

Adding Factory code on request:

 FactoryGirl.define do factory :recurring_transportation_trip, :class => :trip do collection_time "09:00" estimated_duration "60" status "Confirmed" mileage "30" association :collection, :factory => :location association :destination, :factory => :location association :call, :factory => :recurring_call end end 

and for recurring_call

 FactoryGirl.define do factory :recurring_call, :class => "Call" do recurring true recurring_start_date Date.today recurring_end_date Date.today + 1.week recurring_config [1, 3, 5] end end 

------- EDIT2 -------

Turns Trip.count == count + 4 does not actually say anything, and the Trip.count.should == count + 4 test really fails. Thanks @BenediktDeicke for this.

------- EDIT3 -------

In the end, it was a mistake in my application code, and I had to trust the original test from the start. Thanks to everyone who looked. @boulder and @BenediktDeicke are grateful for pointing out the absence of the statement mentioned in edit2.

+4
source share
2 answers

The second test does not actually test anything because it has no expectations.

The Trip.count == count + 4 is just an expression that evaluates to false.

What are you trying to do:

 Trip.count.should == count + 4 

In any case, the first test is one you must trust; it tells you that there is an error in your application code that you should investigate.

+1
source

Chaining is the amount that is expected to change.

 expect{FactoryGirl.create(:recurring_transportation_trip)}.to change(Trip, :count).by(1) 

Here is the documentation .

0
source

All Articles