使用rubyonrailstutorial中的sample_app进行的所有测试的错误

时间:2015-05-17 17:52:51

标签: ruby-on-rails unit-testing ruby-on-rails-4 railstutorial.org

我在6个月前完成了Hartl的rubyonrailstutorial中的sample_app,然后把它放下了。我今天去运行它,现在我似乎在测试中遇到与“setup”功能相关的错误。有45个错误 - 我将在下面发布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 文件(你会注意到我用'thomas'代替'michael':

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

我最近在我的Mac上安装了mysql,但我没有在sample_app中使用它。 sample_app使用本教程中建议的sqlite。我确保当时没有运行mysql的实例。

语句“ArgumentError:错误的参数数量(1表示0)”在每个错误中都是一致的。此外,每个错误似乎都出现在“setup”方法中。

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

1 个答案:

答案 0 :(得分:0)

问题在于&#39; sample_app&#39;中使用的Rails版本。为了公平对待Hartl,他反复声明我们应该使用配置&amp;本教程中显示的gem版本。

我改变了

gem 'rails', '4.2.0.beta4’

gem 'rails', '4.2.1’

然后我跑了bundle update

我跑了bundle exec rake test瞧!,没有错误。 这解决了我原帖中的错误问题,但后来收到了以下警告:

  

DEPRECATION WARNING:配置选项   config.serve_static_assets已重命名为   config.serve_static_files澄清其作用(它只能实现   提供public文件夹中的所有内容,与...无关   资产管道)。将删除serve_static_assets别名   Rails 5.0。请相应地迁移配置文件。   (从座位中调用   [.....] / sample_app /配置/环境/ test.rb:16)

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

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