1

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:increate' test/test_helper.rb:19:in log_in_as' test/integration/users_index_test.rb:29:inblock 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!

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Lee Harris
  • 119
  • 1
  • 5

2 Answers2

1

Your test can't find the record you're looking for because Rails does not load seed.rb into the test database.

You should use something like FactoryGirl, Fixtures or just create the records in the before(:each) block. Any Rails testing tutorial will have something to say about Fixtures, FactoryGirl or some other way to get data into your tests. This may be a good way to start.

Alternatively, if you really need to load what you have in seeds.rb into your testing database, you could try doing this.

Community
  • 1
  • 1
pgaspar
  • 143
  • 1
  • 8
1

Rails works with different environments. While testing your're (duh) on test environment. When running your application locally (with rails server) you are on development environment. When running on a server, it's supposed to be on production environment.

Each environment has its own database. I suppose you ran 'rake db:seed' and thought it'd populate your test database, but it's not true. That's why it isn't finding the record - on test database it doesn't exist.

As pointed out by @pggaspar you should be working with factories or fixtures to populate your test database.

Cristiano Mendonça
  • 1,220
  • 1
  • 10
  • 21
  • Got it - thanks... What confused things was that I had entries in my fixtures, but didn't include the ID field there. Added that, and it works. – Lee Harris May 11 '16 at 17:01