ruby-on-rails - NoMethodError:设置中未定义的方法“用户”
问题描述
也许是一个愚蠢的问题,但是当我尝试在 Ruby 上运行测试时遇到问题,这是错误:
错误:UsersLoginTest#test_login_with_valid_information:
NoMethodError:#UsersLoginTest:0x0000555ef3ec15b0 的未定义方法“用户”您的意思是?超级测试/集成/users_login_test.rb:6:in `setup'
我试图修复夹具 .yml 但我不确定问题是否存在。
这是我的 users.yml
michael:
name: Michael Example
email: michael@example.com
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: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
test "should be valid" do
assert @user.valid?
end
end
问题出在哪里?非常感谢您的帮助
新附件
irb(main):002:0> User.column_names
=> ["id", "name", "email", "created_at", "updated_at", "password_digest"]
我尝试更改 password_digest: 123456,但它引发了同样的错误。我尝试更改密码:test_password,结果如下:
用户测试#test_should_be_valid:
ActiveRecord::Fixture::FixtureError: 表“用户”没有名为“密码”的列。
我还附上了我的 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:create
and rake db:test:prepare
. 现在测试通过了。不要忘记将您的灯具改回:
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
这里的重要教训是test_helper.rb
一些配置和设置的地方,而不是放置测试的地方。
推荐阅读
- angular - 无法使用 angular ng serve 和代理配置 proxy.config.js 为 REST API 后端的身份验证捕获 cookie
- python-2.7 - 无法使用 sentry-sdk 向 Sentry 发送任何内容
- python - Herokuapp 与 Google Cloud 的集成
- python - 使用 netCDF4 从 NOAA 读取 grib2 数据
- data-binding - 在 Alpine.js 中将类绑定到 $el.value
- r - 在美国地图下方移动阿拉斯加和夏威夷
- python - 在 Python 列表中,为什么我附加的值与我附加的值不同?
- reactjs - 无法对 Firestore 集合使用 onSnapshot 方法
- python-3.x - 优化深度优先搜索python
- sql - SQL - 按日期分组防止重复