Rails 单元测试 - 未定义的方法 'users'

Rails unit test - undefined method 'users'

我一直在想办法测试,但似乎无法解决我的问题。每次我尝试 运行 任何测试时,我都会收到此错误:

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

我如何 运行 我在模型中的测试:ruby test/models/plan_test.rb

我正在尝试 运行 最基本的测试(Rails 给我的一个例子),所以它没有链接到任何 model/fixture。 我的模特:

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

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

我也创建了一个空的测试数据库,当我尝试在数据库中添加固定装置时,我遇到了同样的错误。

感谢您的观看:)

编辑: 跟踪夹具命令(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

我的部分用户夹具(它们都具有相同的结构):

one:
  email: ioana1@ioana.com
  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: ioana3@ioana.com
  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

检查您是否有一个名为 app/models/user.rb 的有效模型文件,以及您是否使用 rails g model user 生成了用户模型,或者是否为用户 table 创建了一个迁移,那么你 运行 你的迁移。

RAILS_ENV=test rake db:migrate

您的 app/models/user.rb 至少应包含:

 class User < ActiveRecord::Base
 end

更新:您在下面的评论中说您已经删除了迁移文件。您始终可以通过以下方式让您的测试数据库镜像开发数据库 耙子 db:test:prepare

试试这个:

one:
      email: ioana1@ioana.com
      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