ruby-on-rails - 无方法错误 : undefined method `users' within a setup

标签 ruby-on-rails testing

也许是一个愚蠢的问题,但是当我尝试在 Ruby 上运行测试时遇到问题,这是错误:

Error: UsersLoginTest#test_login_with_valid_information:

NoMethodError: undefined method `User' for #UsersLoginTest:0x0000555ef3ec15b0 Did you mean? super test/integration/users_login_test.rb:6:in `setup'

我尝试修复固定装置 .yml,但我不确定问题是否仍然存在。

这是我的 users.yml

michael:
  name: Michael Example
  email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="600d09030801050c200518010d100c054e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>
  password_digest: <%= User.digest('password') %>

这是我的users_login_test.rb

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end
  
  test "login with valid information" do
    get login_path
    post login_path, params: { session: { email:    @user.email,
                                          password: 'password' } }
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
  end
end

这是我的 test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'

class UserTest < ActiveSupport::TestCase
  fixtures :all
  include ApplicationHelper

  setup do
    @user = User.new(name: "Example User", email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aadfd9cfd8eacfd2cbc7dac6cf84c9c5c7" rel="noreferrer noopener nofollow">[email protected]</a>",
                     password: "foobar", password_confirmation: "foobar")
  end
  test "should be valid" do
    assert @user.valid?
  end
  
end

问题可能出在哪里?非常感谢您的帮助

新附件

irb(main):002:0> User.column_names

=> [“id”、“姓名”、“电子邮件”、“created_at”、“updated_at”、“password_digest”]

我尝试更改password_digest:123456,但它引发了相同的错误。 我尝试更改密码:test_password,结果出现:

UserTest#test_should_be_valid:

ActiveRecord::Fixture::FixtureError: table "users" has no columns named "password".

此外,我还附上了我的 gem 文件,我认为,我故意用于身份验证的唯一文件是 gem 'bcrypt':

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.3'


gem 'rails', '6.0.1'
gem 'autoprefixer-rails', '9.6.1.1'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '5.0.0'
gem 'jquery-rails', '4.3.5'
gem 'mini_magick', '4.9.5'
gem 'will_paginate', '3.2.1'
gem 'bootstrap-will_paginate', '1.0.0'
gem 'bootstrap-sass', '3.4.1'
gem 'puma', '4.3.1'
gem 'font-awesome-rails', '4.7.0.5'
gem 'sass-rails', '6'
gem 'webpacker', '4.0'
gem 'turbolinks', '5'
gem 'jbuilder', '2.9.1'
gem 'rubocop', '0.77.0'
gem 'bootsnap', '1.4.2', require: false
gem 'rails-controller-testing'
gem 'bcrypt',         '3.1.12'

group :development, :test do
  gem 'sqlite3', '1.4.1'
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
end

group :development do
  gem 'web-console', '3.3.0'
  gem 'listen', '3.2.0'
  gem 'spring'
  gem 'spring-watcher-listen', '2.0.0'
end

group :test do
  gem 'capybara',           '3.28.0'
  gem 'selenium-webdriver', '3.142.4'
  gem 'webdrivers',         '4.1.2'
end

group :production do
  gem 'pg',  '0.20.0'
  # gem 'fog', '1.42'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

以防万一我创建了一个存储库:https://github.com/cochabambinoski/Share-Exercise

最佳答案

好吧,您的问题是您将 test_helper.rb 设置为测试本身,这给您带来了问题。您的文件应如下所示:

#test_helper.rb
ENV['RAILS_ENV'] = 'test'
require_relative '../config/environment'
require 'rails/test_help'

class ActiveSupport::TestCase #you were creating a new class in your code
  fixtures :all
  include ApplicationHelper

end

就是这样,克隆您的存储库后我所要做的就是捆绑安装,并执行 rake db:createrake db:test:prepare。现在测试通过了。不要忘记将您的装置改回:

michael:
  name: Michael Example
  email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="224f4b414a43474e62475a434f524e470c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>
  password_digest: <%= User.digest('password') %>

这里的重要教训是 test_helper.rb 是一些配置和设置的地方,而不是放置测试的地方。

关于ruby-on-rails - 无方法错误 : undefined method `users' within a setup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59921356/

相关文章:

ruby-on-rails - Rails - 在没有 url helper 的情况下在模型的虚拟属性中构建绝对 url

ruby-on-rails - Ruby rails 回形针 Seahorse::Client::NetworkingError(返回 SSL_connect=1 errno=0 state=error:证书验证失败)

python - 单元测试和多个模块的 ImportError

ruby - 在 Ruby 中加速 "sleep"

linux - 尝试在 debian linux 上运行 ember 测试时出现奇怪的错误

testing - 如何使用 Protractor 测试生成 jhipster 应用程序

ruby-on-rails - 在 Rails 中,使用 Mongoid,如何找到具有有效(非零)has_one 引用的所有模型?

ruby-on-rails - 如何通过从下拉列表中选择来更改 highcharts 数据值

ruby-on-rails - 使用 Twitter Bootstrap 设计电子邮件联系表单的样式

java - IntelliJ Spring Gradle "Run all tests"不工作(单个测试运行良好)