1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

# 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?