Topic: RSPec Mock going out of scope?
Have an interesting problem with an RSpec mock going out of scope:
# RSpec Question
class Node < ActiveRecord::Base
acts_as_nested_set :scope => :root_id
self.root? ? language.name : parent.language_name
describe Node, "instance" do
@language = mock_model(Language, :name => "Japanese")
@node = Node.create!(:language => @language)
@section1 = Node.create!()
@chapter1 = Node.create!()
it "should return it's own language if it is root" do # Passes
@node.language_name.should == "Japanese"
it "should return it's parent's language if it is a child" do # Fails (message below)
@section1.language_name.should == "Japanese"
@chapter1.language_name.should == "Japanese"
The spec failure I get is:
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
On following the execution with a debugger, I find that it correctly calls language_name recursively up to the root node, then tries to get "language.name" and gets nil. Looking at the parent.language field gives an ID of the mock language so that's not the problem.
Sort of seems like when the recursive call happens to self.language_name, it looses the scope of the Rspec mock @language, especially as the call to the root node in the first spec passes.
Last edited by Mikel Lindsaar (2007-07-18 08:28:39)