I'm still a bit of a newbie, and I'm stumped: I have a 'find_by' statement that works fine when it's executed in a production environment, but it fails during a test, with what is, as far as I can determine, identical input.
I have a very simple table, populated by seeds.rb as follows:
Nonprofit.create!(id: 1, name: "Nonprofit number 1")
Nonprofit.create!(id: 2, name: "Nonprofit number 2")
In my sessions controller, I have the following two statements:
puts "=======>" + params[:nonprofit_id] + "<=============="
nonprofit = Nonprofit.find_by(id: params[nonprofit_id])
puts "=======>" + nonprofit.name + "<=============="
In the production environment, the log shows this:
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Mm7qVchebMORY5QBOlvl1ac69m1ACwttXcE71LbfHnHH2RqpB6mSP+wFxodop/9Mgv/NlLO9V9NVEcTU1a54tw==", "session"=>{"email"=>"jane@rich.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "nonprofit_id"=>"1", "commit"=>"Log in"}
==================>>>1<=========
Nonprofit Load (0.3ms) SELECT `nonprofits`.* FROM `nonprofits` WHERE `nonprofits`.`id` = 1 LIMIT 1
==================>>> Nonprofit number 1
In other words, ActiveRecord finds the correct table entry using the id value of 1.
The log from the test run shows exactly the same output from the 'puts', but then it errors out on the second 'puts' because (presumably) the find_by didn't find the record. Here's the log from the test run:
Running:
==================>>>1<========= E
Finished in 0.467105s, 2.1408 runs/s, 0.0000 assertions/s.
1) Error:
UsersIndexTest#test_index_as_non-admin:
NoMethodError: undefined method name' for nil:NilClass
app/controllers/sessions_controller.rb:14:in
create'
test/test_helper.rb:19:in log_in_as'
test/integration/users_index_test.rb:29:in
block in '
Any ideas/suggestions? Alternatively (or in addition) how can I cause the test run log to show the active record query, like the log shows for the production run?
Thanks!