On Thu, Jan 15, 2009 at 8:20 AM, Andrew Timberlake < [email protected]> wrote:
> On Thu, Jan 15, 2009 at 7:46 AM, Greg Hauptmann < > [email protected]> wrote: > >> PS. Here's an update where I'm at if anyone whats to comment. Not quite >> finished however I'm wondering now if I ensure solid validation level checks >> in model validations (e.g. both ends of an association are working, means >> have to set both ends manually) that I could then rely on Rails to actually >> save both ends of an association even if you only save one. (e.g. seems when >> I save book, chapter also gets saved, and vice-versa). Here's where I'm at: >> >> ---------------------------------------------------------- >> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') >> >> # ----------- BOOK --------------- >> class Book < ActiveRecord::Base >> has_many :chapters >> >> def validate >> if self.chapters.length == 0 >> errors.add_to_base("Book does not have an associated Chapter") >> end >> end >> >> end >> >> # ----------- CHAPTER --------------- >> class Chapter < ActiveRecord::Base >> belongs_to :book >> >> def validate >> if !self.book >> errors.add_to_base("Chapter does not have an associated Book") >> end >> end >> >> end >> >> # --------- RSPEC (BOOK) ------------ >> describe Book do >> before(:each) do >> @valid_attributes = {:amount => 100} >> @b = Book.new(:amount => 100) >> @c = Chapter.new(:amount => 100) >> end >> >> it "should save without error when association is in place" do >> @c.book = @b >> @b.chapters = [...@c] >> @b.save! >> @c.save! >> end >> >> it "should delete without error when both ends of association are >> deleted" do >> @c.book = @b >> @b.chapters = [...@c] >> @b.save! >> @c.save! >> >> @c.destroy >> @b.destroy >> end >> >> it "should fail for SAVE! if there is no association (OBJECT LEVEL)" do >> lambda{ @b.save! }.should raise_error >> end >> >> it "should fail for SAVE! if there is no association (DATABASE LEVEL)" >> do >> @c.book = @b >> @b.chapters = [...@c] >> lambda{ @b.save! }.should_not raise_error #on basis that Rails will >> save Chapter automatically >> end >> >> it "should fail for DELETE if one side left open" do >> @c.book = @b >> @b.chapters = [...@c] >> @b.save! >> @c.save! >> >> lambda{ @b.destroy }.should raise_error >> end >> >> end >> >> # ----------RSPEC (CHAPTER) -------------------- >> describe Chapter do >> before(:each) do >> @valid_attributes = {:amount => 100} >> @b = Book.new(:amount => 100) >> @c = Chapter.new(:amount => 100) >> end >> >> it "should save without error when association is in place" do >> @c.book = @b >> @b.chapters = [...@c] >> @b.save! >> @c.save! >> end >> >> it "should delete without error when both ends of association are >> deleted" do >> @c.book = @b >> @b.chapters = [...@c] >> @b.save! >> @c.save! >> >> @c.destroy >> @b.destroy >> end >> >> it "should fail for SAVE! if there is no association (OBJECT LEVEL)" do >> lambda{ @c.save! }.should raise_error >> end >> >> it "should fail for SAVE! if there is no association (DATABASE LEVEL)" >> do >> @c.book = @b >> @b.chapters = [...@c] >> lambda{ @c.save! }.should_not raise_error # on basis that Rails will >> automatically save Book >> end >> >> it "should fail for DELETE if one side left open" do >> @c.book = @b >> @b.chapters = [...@c] >> @b.save! >> @c.save! >> >> lambda{ @c.destroy }.should raise_error >> end >> >> end >> ---------------------------------------------------------- >> >> $ ./script/autospec >> loading autotest/rails_rspec >> /opt/local/bin/ruby >> /opt/local/lib/ruby/gems/1.8/gems/rspec-1.1.12/bin/spec >> spec/models/all_in_one_test_spec.rb -O spec/spec.opts >> F......... >> >> 1) >> 'Chapter should fail for DELETE if one side left open' FAILED >> expected Exception but nothing was raised >> ./spec/models/all_in_one_test_spec.rb:114: >> >> Finished in 0.280484 seconds >> >> 10 examples, 1 failure >> >> ---------------------------------------------------------- >> >> > Greg, put in a before_delete callback to see if the deletion would leave > the one side open and then return false if it would. > > Sorry, it's before_destroy -- Andrew Timberlake http://ramblingsonrails.com http://www.linkedin.com/in/andrewtimberlake "I have never let my schooling interfere with my education" - Mark Twain --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

