2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RSpecでparams.require(:user).permitエラー

Posted at
users_equest_spec.rb
# 省略
  post users_path, params: { user: FactoryBot.build(:user) }
  expect(is_logged_in?).to be_truthy
# 省略

これで実行すると

  params.require(:user).permit...#省略
  undefined method `permit' for "#<User:0x00007fb5c2fba480>":String

エラー

コントローラのストロングパラメータで使うpermitが定義されてない。
え。。

やったこと

attributes_for
を使う。

コントローラの方はミスないっぽかったのでオブジェクトの方がおかしいと思ったので
rails cで調べてみた。

$ user = FactoryBot.build(:user)
$ user.class
=> User

Userクラス。コントローラにはハッシュを送らなければいけなかった。

$ user2 = FactoryBot.attributes_for(:user)
$ user2.class
=> Hash

なので

users_equest_spec.rb
# 省略
  post users_path, params: { user: FactoryBot.attributes_for(:user) }
  expect(is_logged_in?).to be_truthy
# 省略

これで通った。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?