[rspec-users] Speccing a model class method in Rails produces nil

Martin Hawkins martin.hawkins at gmail.com
Sat Dec 4 09:34:35 EST 2010


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

On Dec 4, 2:25 am, David Chelimsky <dchelim... at gmail.com> wrote:
> On Dec 3, 2010, at 11:07 AM, Martin Hawkins wrote:
>
>
>
>
>
> > 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
>
> Do you actually have 3 identical factories or was that just a copy/paste error?
>
> > 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
>
> The fact that this example ^^ sends SeasonDate the find_start_record message makes the previous example unnecessary.
>
>
>
>
>
> >  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!
>
> Can't really tell from what you've posted. Please post the implementation of SeasonDate.season_date.
> _______________________________________________
> rspec-users mailing list
> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list