■エラーが発生した状況
require 'rails_helper'
RSpec.describe "Albums", type: :request do
let!(:user) { create(:user) }
・・・
describe 'POST /albums' do
・・・
context 'ログインしている場合' do
before do
sign_in user
end
it '記事が保存される' do
album_params = {
title: album.title,
title: Faker::Lorem.characters(number: 10),
pictures: [picture1, picture2]
}
post albums_path**({album: album_params})**
・・・
end
end
end
end
class AlbumsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
def create
@album = current_user.albums.build(album_params)
if @album.save
redirect_to album_path(@album), notice: '保存できたよ'
else
flash.now[:error] = '保存に失敗しました'
render :new
end
end
private
def album_params
params.require(:album).permit(:title, pictures: [])
end
end
■エラーの内容
Since there is no EDITOR or BETTER_ERRORS_EDITOR environment variable, using Textmate by default.
F
Failures:
1) Albums POST /albums ログインしている場合 記事が保存される
**Failure/Error: expect(response).to have_http_status(302)
expected the response to have status code 302 but it was 500**
# ./spec/requests/albums_spec.rb:33:in `block (4 levels) in <top (required)>'
Finished in 3.05 seconds (files took 1.61 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/requests/albums_spec.rb:25 # Albums POST /albums ログインしている場合 記事が保存される
■エラーの原因調査
class AlbumsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
def create
@album = current_user.albums.build(album_params)
**binding.pry**
if @album.save
redirect_to album_path(@album), notice: '保存できたよ'
else
flash.now[:error] = '保存に失敗しました'
render :new
end
end
private
def album_params
params.require(:album).permit(:title, pictures: [])
end
end
bundle exec rspec spec/requests/albums_spec.rb
[1] pry(#<AlbumsController>)> album_params
=> <ActionController::Parameters {"title"=>"r78fa69ajl", "pictures"=>["#Rack::Test::UploadedFile:0x00000001155e5748", "#Rack::Test::UploadedFile:0x00000001155e4488"]} permitted: true>
[1] pry(#<AlbumsController>)> @album.pictures
=> #<ActiveStorage::Attached::Many:0x000000011a706d20 @name="pictures", @record=#<Album:0x000000011c8bd810 id: nil, user_id: 17, title: "zd7kyby9bj", created_at: nil, updated_at: nil>>