[rspec-devel] [ rspec-Bugs-9649 ] Parital mocks don't work if object loaded from fixture, and referenced via association

noreply at rubyforge.org noreply at rubyforge.org
Mon Apr 2 22:01:20 EDT 2007


Bugs item #9649, was opened at 2007-03-28 19:44
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=9649&group_id=797

Category: mock module
Group: None
Status: Open
Resolution: Rejected
Priority: 3
Submitted By: Chris Hoffman (hoffman_c)
Assigned to: Aslak Hellesøy (aslak_hellesoy)
Summary: Parital mocks don't work if object loaded from fixture, and referenced via association

Initial Comment:
Hello,
  The following code will not function properly:

spec/fixtures/pages.yml
----------
first: 
  id: 1
  type: LockedPage
  name: the first default page

spec/fixtures/questions.yml
----------
first: 
  id: 1
  type: LockedQuestion
  name: the first default question
  container_id: 1
  container_type: LockedPage

spec/models/locked_question_spec.rb
----------
context "LockedQuestion#document_session, being linked to a Page that has a Document Session" do
  fixtures :questions, :pages

  setup do
    @question = questions(:first)
    @page = pages(:first)
    @page.stub!(:document_session).and_return("the document session")
    @question.page.should == @page #=> true
  end

  specify "should return the Page's Document Session" do
    @question.document_session.should == "the document session" #=> fails with 'undefined method'
  end
end

I would kludge something together by just assigning an unlocked page to the question, but the "Locked" that prefaces these models is a subclass that denies any modifications.

Thanks for your help.

-Chris


----------------------------------------------------------------------

>Comment By: David Chelimsky (dchelimsky)
Date: 2007-04-03 02:01

Message:
Can we close this then?

----------------------------------------------------------------------

Comment By: Chris Hoffman (hoffman_c)
Date: 2007-04-02 23:05

Message:
Oh, okay, that makes sense.  Thanks for looking into this
issue for me.

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-04-02 20:52

Message:
I was able to reproduce your error. Looking further, it look like @page and @document.page is not the same instance, which explains why the spec is failing:

require File.dirname(__FILE__) + '/../spec_helper'

context "LockedQuestion#document_session, being linked to a Page that has a Document Session" do
  fixtures :questions, :pages

  setup do
    @question = questions(:first)
    @page = pages(:first)
    @page.stub!(:document_session).and_return("the document session")
    # This will work
    #@question.page.stub!(:document_session).and_return("the document session")
  end

  specify "should return the Page's Document Session" do
    @question.page.should == @page #=> true
    @question.page.should equal(@page) #=> fails, which explains why the next one fails too - the stub! is on the wrong object
    @question.page.document_session.should == "the document session" #=> fails with 'undefined method'
  end
end

This seems to be related to Rails not giving you the *same* (equal) page instance - just a similar (==) one.

----------------------------------------------------------------------

Comment By: Chris Hoffman (hoffman_c)
Date: 2007-04-02 17:59

Message:
Hello,

I just ran the project I attached on rails 1.2, and it
exhibited the same results.  If you run into problems
getting that project running, it may be due to an undefined
method 'gem' in config/boot.rb; if you change this to
'require_gem', you should be up and running.

Please let me know if you need additional information.  Just
as a recommendation, it would be superb to have  rspec,
rails, and even ruby version fields in this tracker, so that
people like myself are forced to make this explicit.

Thanks for your help.

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-04-02 14:23

Message:
Your Rails version is one year old, and the oldest Rails version RSpec is tested against is 1.1.6 (8 months old).

It's too much work for us to ensure things work against old Railses, so I'm closing this bug.

Feel free to reopen this bug if it still applies to Rails 1.1.6 or 1.2.x. (In the near future we will probably drop support for 1.1.6 though).


----------------------------------------------------------------------

Comment By: Chris Hoffman (hoffman_c)
Date: 2007-04-02 11:44

Message:
I am not sure how to do partial mocks with Test::Unit.  As
far as versions, I have the following:

rspec 0.8.2
rails 1.1.2

Is the attached rails application not giving you an error
when you run rake spec:models?

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-04-02 11:19

Message:
Can you confirm that you're not getting the same problem when you use Test::Unit?

What are your versions of everything?

----------------------------------------------------------------------

Comment By: Chris Hoffman (hoffman_c)
Date: 2007-04-01 15:24

Message:
Hello,

I have created a rails project with the models and spec that
reveal the problem.  Please let me know if you need
additional information.

Also, you were indeed correct that the above example would
fail for a different reason (if not taking into account the
Question model's delegation).  I apologize for the confusion.

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-04-01 14:25

Message:
What's 'undefined method' getting called on? Question or Page? Can you post the backtrace?

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=9649&group_id=797


More information about the rspec-devel mailing list