4

I have an Organization model that acts_as_nested_set, using awesome_nested_set:

class Organization < ActiveRecord::Base
  acts_as_nested_set
  attr_accessible :name, :location_id, :parent_id

  has_many :org_prod_relationships, dependent: :destroy
  has_many :products, through: :org_prod_relationships

  def has_product?(prod)
    org_prod_relationships.find_by_product_id(prod.id)
  end

  def add_product!(prod)
    org_prod_relationships.create!(product_id: prod.id)
  end

  def publish_product!(prod)
    self.descendants.each do |d|
      d.add_product!(prod)
    end
  end
end

How can I write a test in RSpec for publish_product!, and/or is this the wrong approach to creating org_product_relationships in a nested set, and therefore hard to test? My non-working attempt is here (clipped from the larger spec file) https://gist.github.com/3911555.

EDIT: Updating to include the error message. Note, lines 79 and 80 are:

it { should have_product(product) }
its(:products) { should include(product) }

in the gist.


Failures:

  1) Organization publishes_product 
     Failure/Error: it { should have_product(product) }
       expected #has_product?(#<Product id: 34, name: "floo powder", created_at: "2012-10-21 14:15:08", updated_at: "2012-10-21 14:15:08", photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil>) to return true, got false
     # ./spec/models/organization_spec.rb:79:in `block (3 levels) in <top (required)>'

  2) Organization publishes_product products 
     Failure/Error: its(:products) { should include(product) }
       expected [] to include #<Product id: 35, name: "floo powder", created_at: "2012-10-21 14:15:08", updated_at: "2012-10-21 14:15:08", photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil>
       Diff:
       @@ -1,2 +1,2 @@
       -[#<Product id: 35, name: "floo powder", created_at: "2012-10-21 14:15:08", updated_at: "2012-10-21 14:15:08", photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil>]
       +[]
     # ./spec/models/organization_spec.rb:80:in `block (3 levels) in <top (required)>'

Finished in 29.93 seconds
184 examples, 2 failures

Failed examples:

rspec ./spec/models/organization_spec.rb:79 # Organization publishes_product 
rspec ./spec/models/organization_spec.rb:80 # Organization publishes_product products
thebenedict
  • 2,539
  • 3
  • 20
  • 29

1 Answers1

0

I think you have to reload an object. Try the following:

@organization.products.reload

After you publish products in the before statements. Basically once the @organization is saved, calling @organization.products has a force_reload=false. Reference: http://guides.rubyonrails.org/association_basics.html#has_many-association-reference

Also since you asked, given that you have set up the relationships, you actually should be able to define your methods using the products relationship:

def has_product?(prod)
  products.include? prod
end

def add_product!(prod)
  products << prod
end

I think if you define the methods that way, you may not have to reload because the organization.products association is already updated.

Hope that helps.

Kris Robison
  • 698
  • 7
  • 13