gpt4 book ai didi

ruby-on-rails - Hartl 在 config.serve_static_files 上的 sample_app 警告,并且测试已经定义

转载 作者:行者123 更新时间:2023-12-01 11:34:47 25 4
gpt4 key购买 nike

我正在尝试测试 Hartl 的 sample_app,这是我在运行 bundle exec rake test 后收到的消息:

DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in <top (required)> at /home/aki/sample_app/config/environments/test.rb:16)
rake aborted!
test_should_be_valid is already defined in UserTest
/home/aki/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/testing/declarative.rb:14:in `test'
/home/aki/sample_app/test/models/user_test.rb:10:in `<class:UserTest>'
/home/aki/sample_app/test/models/user_test.rb:3:in `<top (required)>'
/home/aki/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/aki/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
/home/aki/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/aki/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/aki/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/aki/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/aki/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/aki/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/aki/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/aki/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/home/aki/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
(See full trace by running task with --trace)

这是 test/integration/users_profile_test.rb 文件。

require 'test_helper'

class UsersProfileTest < ActionDispatch::IntegrationTest
include ApplicationHelper

def setup
@user = users(:michael)
end

test "profile display" do
get user_path(@user)
assert_template 'users/show'
assert_select 'title', full_title(@user.name)
assert_select 'h1', text: @user.name
assert_select 'h1>img.gravatar'
assert_match @user.microposts.count.to_s, response.body
assert_select 'div.pagination'
@user.microposts.paginate(page: 1).each do |micropost|
assert_match micropost.content, response.body
end
end
end

这是sample_app/config/environments/test.rb 文件:

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true

# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false

# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_assets = true
config.static_cache_control = 'public, max-age=3600'

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false

# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { host: 'localhost:3000' }
# Randomize the order test cases are executed.
config.active_support.test_order = :random

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end

这是test/models/user_test.rb

require 'test_helper'

class UserTest < ActiveSupport::TestCase

def setup
@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

test "name should be present" do
@user.name = " "
assert_not @user.valid?
end

test "email should be present" do
@user.email = " "
assert_not @user.valid?
end

test "name should not be too long" do
@user.name = "a" * 51
assert_not @user.valid?
end

test "email should not be too long" do
@user.email = "a" * 244 + "@example.com"
assert_not @user.valid?
end

test "email validation should accept valid addresses" do
valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
first.last@foo.jp alice+bob@baz.cn]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end

test "email validation should reject invalid addresses" do
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
foo@bar_baz.com foo@bar+baz.com]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
end
end

test "email addresses should be unique" do
duplicate_user = @user.dup
duplicate_user.email = @user.email.upcase
@user.save
assert_not duplicate_user.valid?
end

test "password should have a minimum length" do
@user.password = @user.password_confirmation = "a" * 5
assert_not @user.valid?
end

test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?(:remember, '')
end

test "associated microposts should be destroyed" do
@user.save
@user.microposts.create!(content: "Lorem ipsum")
assert_difference 'Micropost.count', -1 do
@user.destroy
end
end
end

我只是按照教程中的说明进行操作,所以我不知道自己做错了什么。如果您需要其他文件,我会发布它们。gem 版本可能不同还是其他原因?

最佳答案

该教程已过时。您的输出消息显示了两个问题。

1。弃用警告

弃用警告表明 serve_static_assets 是旧名称,新名称是 serve_static_files。 Rails 对名称和方法进行了稳步改进,因此这是一个非常常见的警告。

编辑 sample_app/config/environments/test.rb

来自这里:

config.serve_static_assets  = true

对此:

config.serve_static_files  = true

2。测试重新定义

第二个问题是你的测试文件有问题。

查看错误消息并查看关于您的代码的第一行:

/home/aki/sample_app/test/models/user_test.rb:10:in `<class:UserTest>'

你能把这个文件贴出来吗?

错误信息是说你定义了一个方法两次。

验证

要验证您的更改是否到位,cd 到您的 Rails 根目录。

查找包含旧 Assets 设置的文件名:

find -type f | xargs grep -l 'serve_static_assets' 

查找包含方法名称文本的文件名:

find -type f | xargs grep -l 'test.*should.*be.*valid' 

查看出现的任何文件,看看是否仍有代码需要更改,或者是否有另一个 UserTest 类中的测试“应该有效”。

关于ruby-on-rails - Hartl 在 config.serve_static_files 上的 sample_app 警告,并且测试已经定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28415821/

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