Hartl's sample_app warning on config.serve_sta

2019-02-14 21:32发布

I am trying to test Hartl's sample_app and this is message that I get after running 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)

This is test/integration/users_profile_test.rb file.

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

And this is sample_app/config/environments/test.rb file:

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

This is 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

I am just walking trough instructions from tutorial so I don't know what I am doing wrong.. If you need some other files, I will post them.. Are gem versions different maybe or is it something else?

2条回答
仙女界的扛把子
2楼-- · 2019-02-14 21:54

The tutorial is outdated. Your output message shows two issues.

1. Deprecation Warning

The deprecation warning is shows that serve_static_assets is an old name, and the new name is serve_static_files. Rails makes steady improvements to names and methods, so this is a pretty common warning.

Edit sample_app/config/environments/test.rb.

From this:

config.serve_static_assets  = true

To this:

config.serve_static_files  = true

2. Test redefined

The second issue is that there's a problem with your test file.

Look at the error message and see the top line that is about your code:

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

Can you post this file please?

The error message is saying that you've defined a method twice.

Verify

To verify your changes are in place, cd to your Rails root directory.

Find file names that contain the old assets setting:

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

Find file names that contain the method name text:

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

Look at any files that turn up, and see if there is code that still needs to be changed, or perhaps if there is a test "should be valid" in another UserTest class.

查看更多
Luminary・发光体
3楼-- · 2019-02-14 22:07

If you don't find serve_static_assets in one of your config files, it's likely that one gem is setting this options. So do it:

bundle update

It solved my problem

查看更多
登录 后发表回答