1

大約6個月前,我從Hartl的rubyonrailstutorial完成了sample_app,然後把它放下。我今天去運行它,現在我似乎在我的測試中出現與「設置」功能有關的錯誤。有45個錯誤 - 下面我將他們發佈3:從rubyonrailstutorial使用sample_app的所有測試中的錯誤

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 

這裏是microposts.yml文件中的夾具文件夾(你會注意到,我代替「邁克爾」與'托馬斯:

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 

雖然哈特爾一貫規定我們應該從教程中使用精確的寶石版本,我更新了ruby版本離子從2.1到2.2,我明確地稱它在我的寶石文件。

紅寶石版本:紅寶石2.2.1p85(2015年2月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的實例。

語句「ArgumentError:錯誤的參數數量(1代表0)」在每個錯誤中都是一致的。另外,每個錯誤都出現在「setup」方法中。

這個應用程序工作正常6個月前。關於爲什麼我得到這個錯誤的任何建議?

+0

嘗試改變'@micropost =微柱(:橙色)''到= @micropost Micropost.new(內容: 「Lorem ipsum」,user_id:user.id)''用戶變量的適當初始化。直接以這種方式創建微型後期對象而不是使用燈具可以顯示新的錯誤跟蹤,並幫助確定問題。 –

+0

謝謝。試了這個和其他幾個選擇。沒有工作。經過幾個小時的研究,我所要做的就是更新我的導軌寶石。我發佈了答案。 –

回答

0

問題出在'sample_app'中使用的Rails版本。爲了公平對待Hartl,他一再聲明我們應該使用教程中顯示的配置& gem版本。

我改變

gem 'rails', '4.2.0.beta4’ 

gem 'rails', '4.2.1’ 

然後我跑bundle update

我跑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 [.....]/sample_app/config/environments/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

相關問題