Testing Model That Acts_as_nested_set with RSpec

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 difficult to verify? My non-working attempt here (cropped from a larger spec file) https://gist.github.com/3911555 .

EDIT: update to include error message. Note. Lines 79 and 80:

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

in fact.


 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 
+4
source share
1 answer

I think you need to reload the object. Try the following:

 @organization.products.reload 

After publishing products in before operations. Usually, when @organization is saved, the call to @ organization.products has force_reload = false. Link: http://guides.rubyonrails.org/association_basics.html#has_many-association-reference

Also, as you asked, given that you have established relationships, you really should be able to define your methods using product relationships:

 def has_product?(prod) products.include? prod end def add_product!(prod) products << prod end 

I think that if you define methods this way, you may not need to reboot because the organization.products association has already been updated.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1440540/


All Articles