[rspec-users] How to stub a has_many relationship in Rails 2.3.5

Ben Fyvie ben.fyvie at champsoftware.com
Wed Mar 24 11:39:39 EDT 2010


We have a test that has been working find until we upgraded to rails 2.3.5.
I'm not too familiar with mocks/stubs so maybe there is an easy solution. 

 

Here is a simple example of our scenario. 

 

Class Person < ActiveRecord::Base 

  has_many :aliases, :dependent => :nullify

  before_destroy :mark_aliases_as_deleted

  

  def mark_aliases_as_deleted

    self.aliases.each do |alias|

      alias.mark_as_deleted

    end

  end

 

end

 

This is a test to ensure that when the Person is destroyed that all of their
Aliases get their mark_as_deleted method called. I know this example isn't
very practical, but it is the simplest way I could think of to describe our
problem. 

it "should trigger mark_as_deleted in related aliases" do

  aliases = []

   

  3.times do |i|

    alias = mock("alias #{i}")

    alias.should_receive(:mark_as_deleted)

 

    aliases << alias

  end

 

  @person.stub!(:aliases).and_return(aliases)

 

  @person.destroy

end

 

 

So the problem we are encountering in our scenario is that we get a
NoMethodError exception (undefined method `owner_quoted_id' for
#<Array:0x9a0608c>) when trying to destroy the Person because the stubbed
Aliases return an array and it appears it is expecting some sort of
association object that responds to owner_quoted_id

 

Like I said, we didn't have any problems until upgrading from Rails 2.3.4 to
Rails 2.3.5

 

In case your interested, it is the configure_dependency_for_has_many method
that calls: "#{reflection.primary_key_name} =
\#{record.#{reflection.name}.send(:owner_quoted_id)}"

 

Any help would be greatly appreciated!

 

Ben Fyvie



 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100324/db86f739/attachment-0001.html>


More information about the rspec-users mailing list