直面したエラー
$ docker-compose exec api bin/rspec --only-failure
Failure/Error:
expect(JSON.parse(response.body)).to include({
'user' => {
'id' => user.id,
'user_name' => user.user_name,
'email' => user.email,
}
})
expected {
"user" => {
"created_at" => "2024-05-29T10:00:43.356+09:00",
"deleted_at" => nil,
"email" => "test@gmail.com",
"id" => "123",
"updated_at" => "2024-05-29T10:00:43.356+09:00",
"user_name" => "test_user",
}
}
to include {
"user" => {
"id" => "123",
"user_name" => "test_user",
"email" => "test@gmail.com",
}
}
原因は,include
マッチャはトップレベルのキーのマッチングを行うからでした.
つまり,上例のuser
はinclude
として部分一致でマッチングを行いますが,それ以下のid
,user_name
, email
,updated_at
,deleted_at
,created_at
は全て一致する必要があるためエラーになっていました.
解決策
しかし今回はid
,user_name
,email
で部分一致を行いたかったので,以下で対応しました.
expect(JSON.parse(response.body)['user']).to include({
'id' => user.id,
'user_name' => user.user_name,
'email' => user.email,
'picture' => user.picture
})
最後に
間違っている点があればコメントでご教授いただけると幸いです.