rspec/rspec-core — Issue #562: How to rspec join record in many-to-many in rails 3.1.0
Author: emclab
Created: 2012-01-25 18:16:15+00:00
State: open

Here is the create in rfq controller:

def create
if has_create_right?
@rfq = Rfq.new(params[:rfq], :as => :roles_new )
#save into join table rfqs_standards
params[:rfq][:standard_ids].each do |sid|
@rfq.standards << Standard.find(sid.to_i) if !sid.nil? && sid > 0
end unless params[:rfq][:standard_ids].nil?
if @rfq.save
redirect_to URI.escape("/view_handler?index=0&msg=RFQ saved!")
else
flash.now[:error] = "RFQ not saved!"
render 'new'
end
end
end

Here is the rspec code

describe "'create'" do
it "should be successful for corp head" do
session[:corp_head] = true
session[:user_id] = 1
s = Factory(:standard)
rfq = Factory.attributes_for(:rfq)
rfq.standards << s
get 'create', :rfq => rfq
response.should redirect_to URI.escape("/view_handler?index=0&msg=RFQ saved!")
end
end

The error is:

1) RfqsController 'create' should be successful for corp head
Failure/Error: rfq.standards << s
NoMethodError:
undefined method `standards' for #
# ./spec/controllers/rfqs_controller_spec.rb:65:in `block (3 levels) in