起
Railsチュートリアルの第2章終了時点でテストを実行するとエラーが発生したので、なぜだか調べてみました。
承
実行結果は以下のとおり。2つのfailuresが発生しています。
テストNG
C:\Users\ror\sampleapp>rails t
Run options: --seed 47196
# Running:
..F
Failure:
MicropostsControllerTest#test_should_create_micropost [C:/Users/ror/sampleapp/test/controllers/microposts_controller_test.rb:19]:
"Micropost.count" didn't change by 1.
Expected: 3
Actual: 2
bin/rails test test/controllers/microposts_controller_test.rb:18
.F
Failure:
MicropostsControllerTest#test_should_update_micropost [C:/Users/ror/sampleapp/test/controllers/microposts_controller_test.rb:38]:
Expected response to be a <3XX: redirect>, but was a <200: OK>
bin/rails test test/controllers/microposts_controller_test.rb:36
.........
Finished in 12.588715s, 1.1121 runs/s, 1.3504 assertions/s.
14 runs, 17 assertions, 2 failures, 0 errors, 0 skips
そのときのソースは以下。
test\controllers\microposts_controller_test.rb
require 'test_helper'
class MicropostsControllerTest < ActionDispatch::IntegrationTest
setup do
@micropost = microposts(:one)
end
test "should get index" do
get microposts_url
assert_response :success
end
test "should get new" do
get new_micropost_url
assert_response :success
end
test "should create micropost" do
assert_difference('Micropost.count') do
post microposts_url, params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
end
assert_redirected_to micropost_url(Micropost.last)
end
test "should show micropost" do
get micropost_url(@micropost)
assert_response :success
end
test "should get edit" do
get edit_micropost_url(@micropost)
assert_response :success
end
test "should update micropost" do
patch micropost_url(@micropost), params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
assert_redirected_to micropost_url(@micropost)
end
test "should destroy micropost" do
assert_difference('Micropost.count', -1) do
delete micropost_url(@micropost)
end
assert_redirected_to microposts_url
end
end
app\models\user.rb
class User < ApplicationRecord
has_many :microposts
validates :name, presence: true
validates :email, presence: true
end
app\models\micropost.rb
class Micropost < ApplicationRecord
belongs_to :user
validates :content, length: { maximum: 140 }, presence: true
end
なんでや。
転
ソースを調べていくうちに、
app\models\user.rb
has_many :microposts
app\models\micropost.rb
belongs_to :user
この部分を削ると、エラーが発生しなくなった。
userテーブルとmicropostテーブルが関連していることで発生しているのか・・・?
あ、そうか。
userテーブルにデータがないから、
micropostテーブルにfixtureからデータを入れようとしても入らないのか。
結
setup do内に userテーブルのデータを登録する処理を追加する。
test\controllers\microposts_controller_test.rb
require 'test_helper'
class MicropostsControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:one)
post users_url, params: { user: { name: @user.name, email: @user.email } }
@micropost = microposts(:one)
end
test "should get index" do
get microposts_url
assert_response :success
end
test "should get new" do
get new_micropost_url
assert_response :success
end
test "should create micropost" do
assert_difference('Micropost.count') do
post microposts_url, params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
end
assert_redirected_to micropost_url(Micropost.last)
end
test "should show micropost" do
get micropost_url(@micropost)
assert_response :success
end
test "should get edit" do
get edit_micropost_url(@micropost)
assert_response :success
end
test "should update micropost" do
patch micropost_url(@micropost), params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
assert_redirected_to micropost_url(@micropost)
end
test "should destroy micropost" do
assert_difference('Micropost.count', -1) do
delete micropost_url(@micropost)
end
assert_redirected_to microposts_url
end
end
実行結果エラーがなくなりました。
テストOK
C:\Users\ror\sampleapp>rails t
Run options: --seed 5100
# Running:
..............
Finished in 12.616316s, 1.1097 runs/s, 1.4267 assertions/s.
14 runs, 18 assertions, 0 failures, 0 errors, 0 skips