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?
end
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 #<Hash:0x6741570>
# ./spec/controllers/rfqs_controller_spec.rb:65:in `block (3 levels) in <top (requi
What’s the right way to test join record? Thanks.
From this part of your test:
it would seem that rfq is created as an attributes hash and not a complete model. You can try joining the standard_ids like this:
However, it seems that you can accomplish all of this using nested attributes – see http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html