railsチュートリアルの13章を進めています。
次の流れとしては、コミットとマージなのですが
$rails test を行うとエラーが出て消えません。
「本番環境での画像アップロード」の項はスキップできる
と書いてあったのでスキップしました。
●以下エラー内容
Running via Spring preloader in process 10328 Started with run options --seed 48153 ERROR["test_successful_edit", UsersEditTest, 0.01050325799951679] test_successful_edit#UsersEditTest (0.01s) NameError: NameError: uninitialized constant Micropost::PictureUploader app/models/micropost.rb:4:in `<class:Micropost>' app/models/micropost.rb:1:in `<top (required)>' FAIL["test_micropost_interface", MicropostsInterfaceTest, 1.646527965000132] test_micropost_interface#MicropostsInterfaceTest (1.65s) "Micropost.count" didn't change by 0. Expected: 38 Actual: 39 test/integration/microposts_interface_test.rb:14:in `block in <class:MicropostsInterfaceTest>' FAIL["test_content_should_be_at_most_140_characters", MicropostTest, 1.8568898459998309] test_content_should_be_at_most_140_characters#MicropostTest (1.86s) Expected true to be nil or false test/models/micropost_test.rb:26:in `block in <class:MicropostTest>' FAIL["test_content_should_be_present", MicropostTest, 1.8622546129990951] test_content_should_be_present#MicropostTest (1.86s) Expected true to be nil or false test/models/micropost_test.rb:21:in `block in <class:MicropostTest>' FAIL["test_user_id_should_be_present", MicropostTest, 1.868930400998579] test_user_id_should_be_present#MicropostTest (1.87s) Expected true to be nil or false test/models/micropost_test.rb:16:in `block in <class:MicropostTest>' 50/50: [======================] 100% Time: 00:00:01, Time: 00:00:01 Finished in 1.93830s 50 tests, 260 assertions, 4 failures, 1 errors, 0 skips
なんどもにらめっこしてみたのですが、いまだにどこでミスをしているのか
わかりません。。。
是非ともお力添えいただきたいです。
エラーメッセージに記載されているファイルを載せておきます。
【app/models/micropost.rb】
class Micropost < ApplicationRecord 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 # アップロードされた画像のサイズをバリデーションする def picture_size if picture.size > 5.megabytes errors.add(:picture, "should be less than 5MB") end end end
【test/integration/microposts_interface_test.rb】
require 'test_helper' class MicropostsInterfaceTest < ActionDispatch::IntegrationTest def setup @user = users(:michael) end test "micropost interface" do log_in_as(@user) get root_path assert_select 'div.pagination' # 無効な送信 assert_no_difference 'Micropost.count' do post microposts_path, params: { micropost: { content: "" } } end assert_select 'div#error_explanation' # 有効な送信 content = "This micropost really ties the room together" assert_difference 'Micropost.count', 1 do post microposts_path, params: { micropost: { content: content } } end assert_redirected_to root_url follow_redirect! assert_match content, response.body # 投稿を削除する assert_select 'a', text: 'delete' first_micropost = @user.microposts.paginate(page: 1).first assert_difference 'Micropost.count', -1 do delete micropost_path(first_micropost) end # 違うユーザーのプロフィールにアクセス (削除リンクがないことを確認) get user_path(users(:archer)) assert_select 'a', text: 'delete', count: 0 end end
【test/models/micropost_test.rb】
require 'test_helper' class MicropostTest < ActiveSupport::TestCase def setup @user = users(:michael) @micropost = @user.microposts.build(content: "Lorem ipsum") end test "should be valid" do assert @micropost.valid? end test "user id should be present" do @micropost.user_id = nil assert_not @micropost.valid? end test "content should be present" do @micropost.content = " " assert_not @micropost.valid? end test "content should be at most 140 characters" do @micropost.content = "a" * 141 assert_not @micropost.valid? end test "order should be most recent first" do assert_equal microposts(:most_recent), Micropost.first end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/03 01:12