我正在尝试编写一个 RSpec 系统测试,其中涉及填写页面上的 ActionText / Trix 字段。
好像ActionText::SystemTestHelper
按照定义here https://github.com/rails/rails/blob/master/actiontext/lib/action_text/system_test_helper.rb可以帮助完成这项任务,但到目前为止我还没有成功地实现它。
我的 RSpec 测试如下所示:
# spec/system/add_job_posting_spec.rb
require 'rails_helper'
RSpec.describe 'adding a job posting', type: :system do
let(:user) { FactoryBot.create :user }
before do
login_as(user)
visit new_job_posting_path
end
context 'with valid information' do
let(:valid_job_posting) { FactoryBot.create :job_posting }
scenario 'creates a job posting', js: true do
fill_in 'Title', with: valid_job_posting.title
fill_in_rich_text_area 'Description', with: 'Job Info'
click_button 'Submit'
expect(page).to have_content 'Job was successfully created.'
end
end
end
我还尝试创建一个 RSpec 支持文件
# spec/support/action_text.rb
RSpec.configure do |config|
config.include ActionText::SystemTestHelper, type: :system
end
当我使用它运行时
bundle exec rspec spec/system/add_job_posting_spec.rb
我收到以下错误未初始化的常量 ActionText::SystemTestHelper
当我尝试删除位于spec/support/action_text.rb
我收到此错误:
未定义的方法“fill_in_rich_text_area”
我也尝试过只使用常规的fill_in
RSpec 方法完全避免了这种情况,但随后得到:
找不到未禁用的字段“描述”
尽管该测试被标记为js: true
我还有另一个 RSpec 支持文件来处理设置如下:
# spec/support/system/driver.rb
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
ActiveRecord::Base.establish_connection
driven_by :selenium_chrome_headless
end
end