Hartl对config.serve_static_files的sample_app警告,并且已经定义了测试

我正在尝试测试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  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 `' /home/aki/sample_app/test/models/user_test.rb:3:in `' /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 ' Tasks: TOP => test:run (See full trace by running task with --trace) 

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

 require 'test_helper' class UsersProfileTest 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 `' 

你能发这个文件吗?

错误消息表明您已经定义了两次方法。

校验

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

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

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

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

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

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

如果您在其中一个配置文件中找不到serve_static_assets ,则可能是一个gem正在设置此选项。 这样做:

 bundle update 

它解决了我的问题