Sorry if it is rails talk group issue.
I can't figure how to empty the association_attributes in one of the specs 
for testing if the object is valid.
So, I have the following models:

#timesheet.rb

class Timesheet < ActiveRecord::Base
  attr_accessible :status, :user_id, :start_date, :end_date, 
:activities_attributes
  has_many :activities, dependent: :destroy
  has_many :time_entries, through: :activities
end

#activity.rb
class Activity < ActiveRecord::Base
  attr_accessible :task_id, :timesheet_id, :time_entries_attributes
  validates :task_id, presence: true
  belongs_to :timesheet
  belongs_to :task
  has_many :time_entries, order: :workdate, dependent: :destroy
 accepts_nested_attributes_for :time_entries, allow_destroy: true, 
reject_if: proc { |a| a[:worktime].blank? }
end

I create a timsheet, activity and time entry by using nested forms. It 
works as needed.
But for testing the below spec fails:

#timesheet_spec.rb

describe Timesheet do
  before { @timesheet = build(:submitted_timesheet) }
  
  subject { @timesheet }
...
describe "when activities attributesis empty" do
    before { @timesheet.activities_attributes = {} }
    it { should_not be_valid }
  end
end

The same is in the console:

irb(main):002:0> t = FactoryGirl.build(:submitted_timesheet)
irb(main):003:0> t.activities
=> [#<Activity id: nil, timesheet_id: nil, task_id: 4, created_at: nil, 
updated_at: nil>]
irb(main):004:0> t.valid?
=> true
irb(main):005:0> t.activities_attributes = {}
=> {}
irb(main):006:0> t.activities
=> [#<Activity id: nil, timesheet_id: nil, task_id: 4, created_at: nil, 
updated_at: nil>]
irb(main):007:0> t.valid?
=> true

Any idea on how to 'invalidated' the association_attributes to make it pass 
?

Thanks and regards.

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msg/rspec/-/V_MWu6MR2GIJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to