RSpecでActionTextのテストをする
ActionTextの導入はこちら
zykbgame.hateblo.jp
テストを書く
# 省略 describe '記事の新規投稿' do before do # 投稿ページに移動 visit new_article_path end context '入力値が正常なとき' do before do fill_in 'article_title', with: 'テスト' fill_in 'article_content', with: 'テストです' click_button '記事の作成' end it '記事の新規投稿に成功' do # 省略 end end end
と書くと以下のようなエラー
Failure/Error: fill_in 'article_content', with: 'テストです' Capybara::ElementNotFound: Unable to find field "article_content" that is not disabled

contentはtext_areaではなくrich_text_areaで参照しているため通常のfill_inでは認識されないご様子
<div> <%= f.label :content %> <%= f.rich_text_area :content %> </div>
こんな時のためにActionText::SystemTestHelperにfill_in_rich_text_areaが用意されてるので使えるようにします
# spec/rails_helper.rb # 追加 require "action_text/system_test_helper" RSpec.configure do |config| # 追加 config.include ActionText::SystemTestHelper, type: :system end
fill_in 'article_title', with: 'テスト' # 変更 fill_in_rich_text_area 'article_content', with: 'テストです' click_button '記事の作成'
再度テストします
Finished in 4.91 seconds (files took 2.22 seconds to load) 1 examples, 0 failures
無事テストが通りました