Ioana Surdu Bob
Ioana Surdu Bob

Reputation: 139

Rails unit test - undefined method 'users'

I've been trying to figure out how to test, but nothing seems to solve my issue. Every time I try to run any test, I get this error:

NoMethodError: undefined method `users' for #<#<Class:0x00560775e00768>:0x00560775e00470>

How i run my test inside a model: ruby test/models/plan_test.rb

I am trying to run the most basic test (the one Rails gave me as an example), so it is not linked to any model/fixture. My model:

require File.expand_path("../../test_helper", __FILE__)

class PlanTest < ActiveSupport::TestCase
  test "the truth" do
    assert true
  end
end

I have created an empty test database, too, and when I try to add the fixtures inside the database, I get the same error.

Thank you for looking :)

Edit: Trace on the fixture command(rake db:fixtures:load RAILS_ENV=test --trace):

[DEPRECATION] `last_comment` is deprecated.  Please use `last_description` instead.
** Invoke db:fixtures:load (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:fixtures:load
rake aborted!
NoMethodError: undefined method `users' for #<#<Class:0x005555661d3920>:0x005555661d3830>
(erb):4:in `get_binding'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/2.2.0/erb.rb:863:in `eval'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/2.2.0/erb.rb:863:in `result'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixture_set/file.rb:42:in `render'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixture_set/file.rb:33:in `rows'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixture_set/file.rb:24:in `each'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:760:in `block (2 levels) in read_fixture_files'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixture_set/file.rb:15:in `open'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:759:in `block in read_fixture_files'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:758:in `each'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:758:in `each_with_object'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:758:in `read_fixture_files'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:590:in `initialize'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:514:in `new'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:514:in `block (2 levels) in create_fixtures'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:511:in `map'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:511:in `block in create_fixtures'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:288:in `disable_referential_integrity'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/fixtures.rb:508:in `create_fixtures'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/railties/databases.rake:206:in `block (3 levels) in <top (required)>'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:248:in `call'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:248:in `block in execute'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:243:in `each'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:243:in `execute'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/task.rb:173:in `invoke'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:152:in `invoke_task'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:108:in `each'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:108:in `block in top_level'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:117:in `run_with_threads'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:102:in `top_level'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:80:in `block in run'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:178:in `standard_exception_handling'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/lib/rake/application.rb:77:in `run'
/home/ioana/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/home/ioana/.rbenv/versions/2.2.3/bin/rake:23:in `load'
/home/ioana/.rbenv/versions/2.2.3/bin/rake:23:in `<main>'
Tasks: TOP => db:fixtures:load

Part of my user fixture (all of them have the same structure):

one:
  email: [email protected]
  name: Ioana1
  password: MyString
  verified: 1
  token: MyString
  remember_token: MyString
  plan_id: <%= plan(:p0).id %>
  subscription_id: 1
  subscription_next_bill_date:
  email_hash: 1
  referred_by_user_id: <%= users(:two).id %>
  subscription_status: deleted

three:
  email: [email protected]
  name: Ioana3
  password: MyString
  verified: 1
  token: MyString
  remember_token: MyString
  plan_id: <%= plan(:p1).id %>
  subscription_id: 1
  subscription_next_bill_date: 2016-08-04
  subscription_update_url: MyString
  subscription_cancel_url: MyString
  email_hash: 1
  subscription_registration_date: 2016-07-04
  subscription_status: active

Upvotes: 2

Views: 2397

Answers (2)

Eran Peer
Eran Peer

Reputation: 79

Try This:

one:
      email: [email protected]
      name: Ioana1
      password: MyString
      verified: 1
      token: MyString
      remember_token: MyString
      plan: p0
      subscription_id: 1
      subscription_next_bill_date:
      email_hash: 1
      referred_by_user: two (or the way you named the self reference)
      subscription_status: deleted

http://api.rubyonrails.org/v2.3/classes/Fixtures.html

Upvotes: 1

lacostenycoder
lacostenycoder

Reputation: 11226

Check if you have a valid model file called app/models/user.rb and that if you generated the user model using rails g model user or if you have created a migration for the user table, that you run your migrations.

RAILS_ENV=test rake db:migrate

Your app/models/user.rb should contain at the very least:

 class User < ActiveRecord::Base
 end

UPDATE: Your comment below says you've deleted the migration file. You can always get your test DB to mirror development DB by doing rake db:test:prepare

Upvotes: 0

Related Questions