gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:41:13 24 4
gpt4 key购买 nike

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

Error: UsersLoginTest#test_login_with_valid_information:


NoMethodError: undefined method `User' for #UsersLoginTest:0x0000555ef3ec15b0 Did you mean? supertest/integration/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,结果出现了:

UserTest#test_should_be_valid:

ActiveRecord::Fixture::FixtureError: table "users" has no columnsnamed "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: michael@example.com
password_digest: <%= User.digest('password') %>

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com