Joaquin Rivera Padron
2010-Mar-12 12:45 UTC
[rspec-users] spec-ing a nested object is saved on the specs of the object it is nested into
hi there, I have a Rails model A, that has_one model B, model A have a method called save_nested_b that: * context ''invalid B params'' ** context ''no nested B exists'' => do nothing ** context ''nested B exists'' => remove it * context ''valid B params'' ** context ''no nested B exists'' => create it ** context ''nested B exists'' => remove it and create new one (or edit attributes linking to A) the save_nested_b method implementation should not be problematic. My question is: on A specs I want to make sure all above on the list happens but not having to repeat all B validations on making ''invalid B params'' examples, because B have its own specs, and also the A specs for save_nested_b will be brittle when B validations change (not to mention the combinations of invalid fields can be long) how would you do this? would you do a loose thing like: context ''invalid B params'' do .. a.save_nested_b nil a.should have_no_b .. end giving it a more thought after writing this email I guess I would go for this approach, what do you think? greetings, joaquin -- www.least-significant-bit.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100312/012c16eb/attachment.html>
David Chelimsky
2010-Mar-15 13:15 UTC
[rspec-users] spec-ing a nested object is saved on the specs of the object it is nested into
On Fri, Mar 12, 2010 at 7:45 AM, Joaquin Rivera Padron <joahking at gmail.com> wrote:> hi there, > I have a Rails model A, that has_one model B, model A have a method called > save_nested_b that: > * context ''invalid B params'' > ?** context ''no nested B exists'' => do nothing > ?** context ''nested B exists'' => remove it > > * context ''valid B params'' > ?** context ''no nested B exists'' => create it > ?** context ''nested B exists'' => remove it and create new one (or edit > attributes linking to A) > > the save_nested_b method implementation should not be problematic. My > question is: > > on A specs I want to make sure all above on the list happens but not having > to repeat all B validations on making ''invalid B params'' examples, because B > have its own specs, and also the A specs for save_nested_b will be brittle > when B validations change (not to mention the combinations of invalid fields > can be long) > > how would you do this? would you do a loose thing like: > > context ''invalid B params'' do > .. > a.save_nested_b nil > a.should have_no_b > .. > end > > giving it a more thought after writing this email I guess I would go for this approach, what do you think?Depends on who is calling save_nested_b. I usually save associations via callbacks as part of the save operation, and wouldn''t call save_nested_b from outside the A object. In that case, I''d treat save_nested_b as a private method and likely not spec it directly. i.e. a = A.create!(valid_a_attributes, :b => valid_b_attributes) a.should have_a_b a = A.create!(valid_a_attributes, :b => invalid_b_attributes) a.should have_no_b etc WDYT? David
Joaquin Rivera Padron
2010-Mar-15 13:35 UTC
[rspec-users] spec-ing a nested object is saved on the specs of the object it is nested into
thanks for your reply David, what worried me (well, got me thinking) was having to put on A''s specs too much knowledge about when B''s attributes were valid or not. I ended up doing more or less what you expose, but only: * using nil as the invalid input * using one valid combination for the valid ones greetings, joaquin 2010/3/15 David Chelimsky <dchelimsky at gmail.com>> On Fri, Mar 12, 2010 at 7:45 AM, Joaquin Rivera Padron > <joahking at gmail.com> wrote: > > hi there, > > I have a Rails model A, that has_one model B, model A have a method > called > > save_nested_b that: > > * context ''invalid B params'' > > ** context ''no nested B exists'' => do nothing > > ** context ''nested B exists'' => remove it > > > > * context ''valid B params'' > > ** context ''no nested B exists'' => create it > > ** context ''nested B exists'' => remove it and create new one (or edit > > attributes linking to A) > > > > the save_nested_b method implementation should not be problematic. My > > question is: > > > > on A specs I want to make sure all above on the list happens but not > having > > to repeat all B validations on making ''invalid B params'' examples, > because B > > have its own specs, and also the A specs for save_nested_b will be > brittle > > when B validations change (not to mention the combinations of invalid > fields > > can be long) > > > > how would you do this? would you do a loose thing like: > > > > context ''invalid B params'' do > > .. > > a.save_nested_b nil > > a.should have_no_b > > .. > > end > > > > giving it a more thought after writing this email I guess I would go for > this approach, what do you think? > > Depends on who is calling save_nested_b. I usually save associations > via callbacks as part of the save operation, and wouldn''t call > save_nested_b from outside the A object. In that case, I''d treat > save_nested_b as a private method and likely not spec it directly. > i.e. > > a = A.create!(valid_a_attributes, :b => valid_b_attributes) > a.should have_a_b > > a = A.create!(valid_a_attributes, :b => invalid_b_attributes) > a.should have_no_b > > etc > > WDYT? > > David > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- www.least-significant-bit.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100315/6166d699/attachment.html>