[rspec-users] Rails - Mock going out of scope?

Mikel Lindsaar raasdnil at gmail.com
Wed Jul 18 18:56:15 EDT 2007

Hello list,

I think I have a rails related RSpec problem with a mock going out of
scope on a recursive call to a model.

The code is at: http://pastie.textmate.org/79821 if you want to see it
highlighted.  I have pasted it below as well.

Basically, I have an acts_as_nested_set model called "Node", which
works fine.  I have a function which finds the language name of the
node instance.  If the language is nil for the node instance being
queried, it then recursively calles language_name on it's parent until
one of them has the language.  Then this gets returned.

When I do this with a fixture, it works fine.  Ie, a Database call can
be made to a language table and I get the language name.

In the code attached it has a langauge instance being mocked.  I get
the same result if I mock Language.should_receive(:find)...

It SEEMS like the Mock is going out of scope on the recursive call to
parent.  The direct spec to the parent to get language name works

Any ideas? (the code below is slimmed down to the code needed to run the spec.




class Node < ActiveRecord::Base

  belongs_to :language
  acts_as_nested_set :scope => :root_id

  def language_name
    self.root? ? language.name : parent.language_name

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!()

  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"


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

More information about the rspec-users mailing list