I''m learning Ruby and Rails at the same time, so I admit this is
probably just a syntax thing. I''m getting a NoMethodError: undefined
method `[]'' for nil:NilClass in my controller test.
Sales has_many :items and Items belong_to :sale. I''m setting up a
before_filter to instantiate my sale from the params for several Item
actions.
,----[ app/controllers/item_controller.rb ]
| class ItemController < ApplicationController
| model :item
| scaffold :item
| before_filter( :only => :new ) { @sale =
Sale.find(@params["sale_id"]) }
| end
`----
,----[ test/functional/item_controller_test.rb ]
| class ItemControllerTest < Test::Unit::TestCase
| fixtures :sales
|
| def setup
| @controller = ItemController.new
| @request = ActionController::TestRequest.new
| @response = ActionController::TestResponse.new
| end
|
| def test_sale_id
| get :new, { "sale_id" => "1" }
| assert_success
| assert_template_has ''sale''
| end
| end
`----
When I run the test, it fails with "NoMethodError: undefined method
`[]'' for nil:NilClass" in item_controller.rb:4. I''m
assuming that
@params is nil here instead of the hash I''m expecting. I''ve
tried a
couple of variations on that get call:
get :new, :sale_id => 1
get :new, :params => { "sale_id" => 1 }
I''m just not sure what the proper syntax is. Of course, my filter
block could be bad too.
--
doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org