0
votes

This particular test is trying to create a 'status update' for a user.

Here is the full error:

 Failure/Error: FactoryGirl.create(:status_update, user: @user, created_at: 1.day.ago)
 NoMethodError:
   undefined method `*' for nil:NilClass
 # ./app/models/status_update.rb:31:in `default_values'
 # ./spec/models/user_spec.rb:28:in `block (3 levels) in <top (required)>'

Here is the test:

describe "Status Update Associations" do
before { @user.save }
let!(:older_status_update) do 
  FactoryGirl.create(:status_update, user: @user, created_at: 1.day.ago)
end
let!(:newer_status_update) do 
  FactoryGirl.create(:status_update, user: @user, created_at: 1.hour.ago )
end

it "should have status updates in the right order" do 
  @user.status_update.should == [newer_status_update, older_status_update]
end
end     

Since the error is pointing to the status update model I might as well include that here as well. I suspect it's got something to do with some variables being set after initialization and the let! in the test, although I'm stumped with trying different callbacks.

class StatusUpdate < ActiveRecord::Base
   belongs_to :user

   after_initialize :default_values

   attr_accessible :current_weight,
                   :current_bf_pct,
                   :current_lbm,
                   :current_fat_weight,
                   :change_in_weight,
                   :change_in_bf_pct,
                   :change_in_lbm,
                   :change_in_fat_weight,
                   :total_weight_change,
                   :total_bf_pct_change,
                   :total_lbm_change,
                   :total_fat_change

   validates :user_id, presence: true
   validates :current_bf_pct, presence: true,
                              numericality: true,
                              length: { minimum: 4, maximum:5 }  
   validates :current_weight, presence: true,
                              numericality: true,
                              length: { minimum: 4, maximum:5 } 
   validates :current_lbm, presence: true
   validates :current_fat_weight, presence: true                   

   def default_values
     self.current_fat_weight = self.current_weight * self.current_bf_pct
     self.current_lbm = self.current_weight - self.current_fat_weight
   end  

   default_scope order: 'status_update.created_at DESC'
end         

Here is the factory that adds the 'current_weight and current_bf_pct to the default_values method.

 factory :status_update do
user
  current_weight 150
  current_bf_pct 0.15
end         

Thanks!

1

1 Answers

1
votes

It's due to your default_values method.

You're doing self.current_weight * self.current_bf_pct but none of them are set to a numerical value.