LoginSignup
0
1

More than 1 year has passed since last update.

単体テスト用に ActionView::Helpers::FormBuilder オブジェクトを生成する

Last updated at Posted at 2022-05-17

方法

class DummyFormBuilder < ActionView::Helpers::FormBuilder
  # 引数に Active Record を渡す前提。
  def initialize(model_object)
    object_name = model_object.model_name.param_key
    template = ActionView::Base.empty
    options = {}

    super(object_name, model_object, template, options)
  end
end
# user #=> #<User id: 1, ..., updated_at: "2022-01-01 00:00:00.000000000 +0900">
# user.model_name.param_key #=> "user"
let!(:f) { DummyFormBuilder.new(user) }

バージョン情報

  • Ruby 2.7.6
  • Rails 6.1.6

参考

Rails

0
1
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
0
1