Another shot in the dark:
My guess is that there is `activities_attributes` method to begin with and
Rails generates it for you the first time it is called. By setting
timesheet.activities_attributes = {}, you're assigning a value to a
previously non-existent variable that is not used internally in validations.
Part of the problem here, IMO, is an overuse of "reuse". You're creating a
valid instance and then changing its state in order to make it invalid. I
think it would be easier to understand if you created an invalid instance,
e.g.
describe Timesheet do
it "is invalid when any of its activities has no tasks" do
timesheet = Timesheet.new
timesheet.activities << Activity.new
expect(timesheet).not_to be_valid
end
end
You can do that using subject and/or before if you like, but this is very
explicit and easy to reason about as/is.
On Monday, March 25, 2013 7:39:12 AM UTC-5, Dan Brooking wrote:
>
> Just a shot in the dark as I'm not a rails expert... But it doesn't look
> like your code required timesheet to have activities_attributes?
>
> Do you need to add this:
> validates :activities_attributes, presence: true
>
> to the model?
>
> On Monday, March 25, 2013, Javix wrote:
>
>>
>>
>>> Please post the failure message.
>>>
>>
>> describe "when activities attributes is empty" do
>> before { @timesheet.activities_attributes = {} }
>> it { should_not be_valid }
>> end
>> ...
>>
>> Output:
>>
>> when activities attributes is empty
>> should not be valid (FAILED - 1)
>> ...
>>
>> Failures:
>>
>> 1) Timesheet when activities attributes is empty
>> Failure/Error: it { should_not be_valid }
>> expected valid? to return false, got true
>> # ./spec/models/timesheet_spec.rb:39:in `block (3 levels) in <top
>> (required)>'
>>
>> Finished in 2.69 seconds
>> 45 examples, 1 failure
>>
>> Failed examples:
>>
>> rspec ./spec/models/timesheet_spec.rb:39 # Timesheet when activities
>> attributes is empty
>>
>> The only way I found for the moment is to build a timesheet with ONe and
>> the only activity, then setting its task to nil as follows:
>>
>> describe "when the task is not present" do
>> before { @timesheet.activities.first.task = nil }
>> it { should_not be_valid }
>> end
>>
>> Far from being perfect, agreed. So any idea would be really appreciated.
>> 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/-/18oHzvGc1GYJ.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
--
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/-/pGAJvG4SkiMJ.
For more options, visit https://groups.google.com/groups/opt_out.