如何使用RSpec测试ActionText? [英] How to test ActionText using RSpec?
问题描述
我正在尝试编写一个RSpec系统测试,该测试涉及在页面上填写一个ActionText/Trix字段.
I am trying to write an RSpec system test that involves filling out an ActionText / Trix field on a page.
在此处定义的情况下,似乎ActionText::SystemTestHelper
可以帮助完成此任务,但是到目前为止,我在实现它方面没有任何成功.
It seems as though ActionText::SystemTestHelper
as defined here is available to help with this task but so far I have not had any success in implementing it.
我的RSpec测试如下所示:
My RSpec test looks like the following:
# 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支持文件
I have also tried to create a RSpec support file as such
# 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
I get the following error uninitialized constant ActionText::SystemTestHelper
当我尝试删除位于spec/support/action_text.rb
的文件时,出现此错误:
When I try to remove the file located at spec/support/action_text.rb
I get this error instead:
未定义的方法"fill_in_rich_text_area"
undefined method `fill_in_rich_text_area'
我还尝试过使用常规的fill_in
RSpec方法来完全避免这种情况,但是可以得到:
I have also tried to just use the regular fill_in
RSpec method to avoid that entirely but then get:
无法找到未禁用的字段"Description"
Unable to find field "Description" that is not disabled
尽管测试被标记为js: true
,但我还有另一个RSpec支持文件来处理该设置,如下所示:
Despite the fact that the test is marked as js: true
and I have another RSpec support file to handle that which is setup as follows:
# 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
推荐答案
这是我正在使用的脚本(只需将其作为辅助工具即可):
Here’s the script I’m using (just include it as a helper):
def fill_in_trix_editor(id, with:)
find(:css, "##{id}").click.set(with)
end
ActionText创建带下划线的model +属性ID.您还可以检查看看.
ActionText creates an underscored id of the model + attribute. You can also inspect to see.
这篇关于如何使用RSpec测试ActionText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!