Speccing a model class method in Rails produces nil

Ruby 1.9.2, Rails 3.0.3, Rspec-rails 2.2.0

I have:

Factory.define :season_date do |f|
f.season_date Date.new(2011,9,24)
f.date_type “season_start”
end

RSpec.configure do |config|
config.mock_with :rspec
end

Factory.define :season_date do |f|
f.season_date Date.new(2011,9,24)
f.date_type “season_start”
end

Factory.define :season_date do |f|
f.season_date Date.new(2011,9,24)
f.date_type “season_start”
end

require ‘spec_helper’
describe SeasonDate do
before(:each) do
@start_date_record = Factory.create(:season_date)
@no_play_date_record = Factory.create(:season_date, season_date:
Date.today, date_type: “no_play”)
end
it “responds to the find_start_record method call” do
SeasonDate.should respond_to(:find_start_record)
end
it “returns the record with the season start date” do
SeasonDate.find_start_record.should == @start_date_record
end
end

and I get

rspec -f d -b spec/models/season_date_spec.rb
SeasonDate
responds to the find_start_record method call
returns the record with the season start date (FAILED - 1)

Failures:

  1. SeasonDate returns the record with the season start date
    Failure/Error: SeasonDate.find_start_record.should ==
    @start_date_record
    expected: #<SeasonDate id: 66, season_date: “2011-09-24”,
    date_type: “season_start”, created_at: “2010-12-03 17:03:58”,
    updated_at: “2010-12-03 17:03:58”>,
    got: nil (using ==)

I’m doing something dumb, but I don’t know what. Any assistance much
appreciated!

On Dec 3, 2010, at 11:07 AM, Martin H. wrote:

config.mock_with :rspec
end
Do you actually have 3 identical factories or was that just a copy/paste
error?

SeasonDate.find_start_record.should == @start_date_record
The fact that this example ^^ sends SeasonDate the find_start_record
message makes the previous example unnecessary.

Failures:
appreciated!
Can’t really tell from what you’ve posted. Please post the
implementation of SeasonDate.season_date.

I can’t believe I did that…
No, there is only one Factory; copy and paste error. The SeasonDate
class is:
class SeasonDate < ActiveRecord::Base
def self.find_start_record
where([“date_type = ?”, “start_date”]).first
end
end
Agreed regarding the redundant test - I just put that in when things
went wrong.
Thanks for responding

Apologies if this is a repeat post but google seemed to ignore my last
effort.
There is only one Factory - copy, paste and editing error.
I can’t believe that I didn’t include the code being tested…

class SeasonDate < ActiveRecord::Base
def self.find_start_record
where([“date_type = ?”, “start_date”]).first
end
end

Agreed re redundant test - I put that in when the ‘real’ test failed.

I can’t believe I did that either…
Doh!

On Sat, Dec 4, 2010 at 8:34 AM, Martin H.
[email protected] wrote:

I can’t believe I did that…
No, there is only one Factory; copy and paste error. The SeasonDate
class is:
class SeasonDate < ActiveRecord::Base
def self.find_start_record
where([“date_type = ?”, “start_date”]).first

I think the problem is “start_date” vs “season_start”. The factory says:

Factory.define :season_date do |f|
f.season_date Date.new(2011,9,24)
f.date_type “season_start”
end

date_type == “start_date” # in the implementation
date_type == “season_start” # in the factory

HTH,
David