使用来自 ruby​​onrailstutorial 的 sample_app 的所有测试的错误

Errors on all tests using sample_app from rubyonrailstutorial

我大约 6 个月前完成了 Hartl 的 rubyonrailstutorial 的 sample_app,然后放下了。我今天去了 运行 它,现在我似乎在我的测试中收到与 "setup" 函数相关的错误。有 45 个错误 - 我会 post 下面是其中的 3 个:

ERROR["test_should_redirect_create_when_not_logged_in", MicropostsControllerTest, 2015-05-17 12:55:23 -0400]
 test_should_redirect_create_when_not_logged_in#MicropostsControllerTest (1431881723.02s)
ArgumentError:         ArgumentError: wrong number of arguments (1 for 0)
            test/controllers/microposts_controller_test.rb:6:in `setup'
        test/controllers/microposts_controller_test.rb:6:in `setup'

ERROR["test_should_redirect_destroy_for_wrong_micropost", MicropostsControllerTest, 2015-05-17 12:55:23 -0400]
 test_should_redirect_destroy_for_wrong_micropost#MicropostsControllerTest (1431881723.02s)
ArgumentError:         ArgumentError: wrong number of arguments (1 for 0)
            test/controllers/microposts_controller_test.rb:6:in `setup'
        test/controllers/microposts_controller_test.rb:6:in `setup'

ERROR["test_should_redirect_destroy_when_not_logged_in", MicropostsControllerTest, 2015-05-17 12:55:23 -0400]
 test_should_redirect_destroy_when_not_logged_in#MicropostsControllerTest (1431881723.03s)
ArgumentError:         ArgumentError: wrong number of arguments (1 for 0)
            test/controllers/microposts_controller_test.rb:6:in `setup'
        test/controllers/microposts_controller_test.rb:6:in `setup'

这里是micropost_controller_test.rb:

require 'test_helper'

class MicropostsControllerTest < ActionController::TestCase

  def setup
    @micropost = microposts(:orange)   #THIS IS LINE 6
  end

  test "should redirect create when not logged in" do
    assert_no_difference 'Micropost.count' do
      post :create, micropost: { content: "Lorem ipsum" }
    end
    assert_redirected_to login_url
  end

  test "should redirect destroy when not logged in" do
    assert_no_difference 'Micropost.count' do
      delete :destroy, id: @micropost
    end
    assert_redirected_to login_url
  end

  test "should redirect destroy for wrong micropost" do
    log_in_as(users(:thomas))
    micropost = microposts(:ants)
    assert_no_difference 'Micropost.count' do
      delete :destroy, id: micropost
    end
    assert_redirected_to root_url
  end

end

这是 fixtures 文件夹中的 microposts.yml 文件(您会注意到我将 'michael' 替换为 'thomas':

orange:
  content: "I just ate an orange!"
  created_at: <%= 10.minutes.ago %>
  user: thomas

tau_manifesto:
  content: "Check out the @tauday site by @mhartl: http://tauday.com"
  created_at: <%= 3.years.ago %>
  user: thomas

cat_video:
  content: "Sad cats are sad: http://youtu.be/PKffm2uI4dk"
  created_at: <%= 2.hours.ago %>
  user: thomas

most_recent:
  content: "Writing a short test"
  created_at: <%= Time.zone.now %>
  user: thomas

<% 30.times do |n| %>
micropost_<%= n %>:
  content: <%= Faker::Lorem.sentence(5) %>
  created_at: <%= 42.days.ago %>
  user: thomas
<% end %>

ants:
  content: "Oh, is that what you want? Because that's how you get ants!"
  created_at: <%= 2.years.ago %>
  user: archer

zone:
  content: "Danger zone!"
  created_at: <%= 3.days.ago %>
  user: archer

tone:
  content: "I'm sorry. Your words made sense, but your sarcastic tone did not."
  created_at: <%= 10.minutes.ago %>
  user: lana

van:
  content: "Dude, this van's, like, rolling probable cause."
  created_at: <%= 4.hours.ago %>
  user: lana

这里是micropost.rb模型,

class Micropost < ActiveRecord::Base
  belongs_to :user
  default_scope -> { order(created_at: :desc) }
  mount_uploader :picture, PictureUploader
  validates :user_id, presence: true
  validates :content, presence: true, length: { maximum: 140 }
  validate  :picture_size

  private

    # Validates the size of an uploaded picture.
    def picture_size
      if picture.size > 5.megabytes
        errors.add(:picture, "should be less than 5MB")
      end
    end
end

这里是microposts_controller.rb:

class MicropostsController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]
  before_action :correct_user,   only: :destroy

  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_url
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

  def destroy
    @micropost.destroy
    flash[:success] = "Micropost deleted"
    redirect_to request.referrer || root_url
  end

  private

    def micropost_params
      params.require(:micropost).permit(:content, :picture)
    end

    def correct_user
      @micropost = current_user.microposts.find_by(id: params[:id])
      redirect_to root_url if @micropost.nil?
    end
end

这里是gemfile.rb:

source 'https://rubygems.org'
ruby '2.2.1'

gem 'rails', '4.2.0.beta4'     #<<<THIS WAS THE PROBLEM.  ANSWER BELOW
gem 'bcrypt'
gem 'faker', '1.4.2'
#Items for image upload
gem 'carrierwave',             '0.10.0'
gem 'mini_magick',             '3.8.0'
gem 'fog',                     '1.23.0'

gem 'will_paginate'
gem 'bootstrap-will_paginate'
gem 'bootstrap-sass'
gem 'sass-rails', '5.0.0.beta1'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '4.0.0.beta2'
gem 'turbolinks',   '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'rails-html-sanitizer', '1.0.1'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '0.4.0',          group: :doc


group :development, :test do
    gem 'sqlite3'
    gem 'spring'
    gem 'byebug',      '3.4.0'
  gem 'web-console', '2.0.0.beta3'
end

group :test do
  gem 'minitest-reporters', '1.0.5'
  gem 'mini_backtrace',     '0.1.3'
  gem 'guard-minitest',     '2.3.1'
end

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
  gem 'unicorn',                '4.8.3'
end

虽然 Hartl 一直声明我们应该使用教程中的 gem 版本,但我确实将 ruby 版本从 2.1 更新到 2.2,并且我在 gem 中明确调用了它] 文件。

Ruby 版本: ruby 2.2.1p85(2015-02-26 修订版 49769)[x86_64-darwin13]

Rails 版本: Rails 4.2.0.beta4

系统: Mac OS X 版本 10.9.5

我最近将 mysql 安装到我的 Mac,但我没有在 sample_app 中使用它。 sample_app 按照教程中的建议使用 sqlite。我确保 mysql 的实例当时不是 运行ning。

声明"ArgumentError: wrong number of arguments (1 for 0)"在每个错误中都是一致的。此外,每个错误似乎都发生在 "setup" 方法中。

此应用在 6 个月前运行良好。关于我为什么会收到此错误的任何建议?

问题出在 'sample_app' 中使用的 Rails 版本。公平地说,Hartl 反复声明我们应该使用教程中显示的配置和 gem 版本。

我改了

gem 'rails', '4.2.0.beta4’

gem 'rails', '4.2.1’

那我运行bundle update

I 运行 bundle exec rake testvoila!,没有错误。 这解决了我原来post中的错误问题,但随后我收到以下警告:

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 [.....]/sample_app/config/environments/test.rb:16)

这需要更新 /config/environments/test.rb 文件。改变了 config.serve_static_assets = trueconfig.serve_static_files = true

有关解决方案的详细信息,请参阅以下 link: Hartl's sample_app warning on config.serve_static_files, and test already defined