Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
# RSpec Question class Node < ActiveRecord::Base belongs_to :language acts_as_nested_set :scope => :root_id def language_name self.root? ? language.name : parent.language_name end end describe Node, "instance" do fixtures :nodes before(:each) do @language = mock_model(Language, :name => "Japanese") @node = Node.create!(:language => @language) @section1 = Node.create!() @chapter1 = Node.create!() end it "should return it's own language if it is root" do # Passes @language.should_receive(:name).exactly(:once).and_return("Japanese") @node.language_name.should == "Japanese" end it "should return it's parent's language if it is a child" do # Fails (message below) @section1.move_to_child_of(@node) @chapter1.move_to_child_of(@section1) @language.should_receive(:name).exactly(:once).and_return("Japanese") @section1.language_name.should == "Japanese" @language.should_receive(:name).exactly(:once).and_return("Japanese") @chapter1.language_name.should == "Japanese" end end # NoMethodError in 'Node instance should return it's parent's language if it is a child' # You have a nil object when you didn't expect it! # The error occurred while evaluating nil.name # /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in `language_name' # /Users/mikel/working/universal_translator/config/../app/models/node.rb:29:in `language_name' # ./spec/models/node_spec.rb:160: # script/spec:4: # On looking, I find that it correctly calls itself up to parent, then tries to get # "language.name" and gets nil because language no longer is defined? How can I get the # @language to persist?
This paste will be private.
From the Design Piracy series on my blog: