mardi 28 avril 2015

Lost trying to make the rspec test pass

I have this class:

class Zombie < ActiveRecord::Base
  attr_accessible :iq
  validates :name, presence: true

  def genius?
    iq >= 3
  end

  def self.genius
    where("iq >= ?", 3)
  end
end

And I am making the rspec test:

describe Zombie do
  context "with high iq" do
     let!(:zombie) { Zombie.new(iq: 3, name: 'Anna') }
     subject { zombie }

     it "should be returned with genius" do
       should be_genius
     end

     it "should have a genius count of 1" do
       Zombie.genius.count.should == 1 
     end
  end
end

I am having this error message:

Failures:

1) Zombie with high iq should have a genius count of 1
Failure/Error: Zombie.genius.count.should == 1
expected: 1
got: 0 (using ==)
# zombie_spec.rb:11:in `block (3 levels) '

Finished in 0.2138 seconds
2 examples, 1 failure

Failed examples:

rspec zombie_spec.rb:10 # Zombie with high iq should have a genius count of 1 

I am using the syntax: let!(:zombie){...} but it is telling that got 0 when I expected 1. Any idea? Maybe I have passed a lot of time looking to this code and I don't see where is the problem.

Aucun commentaire:

Enregistrer un commentaire