如何使用 RSpec 测试 ActionText?

Mho*_*oad 7 rspec ruby-on-rails trix ruby-on-rails-6 actiontext

我正在尝试编写一个 RSpec 系统测试,该测试涉及在页面上填写 ActionText / Trix 字段。

似乎此处ActionText::SystemTestHelper定义的可用于帮助完成此任务,但到目前为止我还没有成功实现它。

我的 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
Run Code Online (Sandbox Code Playgroud)

我还尝试创建一个 RSpec 支持文件

# spec/support/action_text.rb

RSpec.configure do |config|
  config.include ActionText::SystemTestHelper, type: :system
end
Run Code Online (Sandbox Code Playgroud)

当我使用

bundle exec rspec spec/system/add_job_posting_spec.rb
Run Code Online (Sandbox Code Playgroud)

我收到以下错误 未初始化的常量 ActionText::SystemTestHelper

当我尝试删除位于 的文件时,spec/support/action_text.rb我收到此错误:

未定义的方法‘fill_in_rich_text_area’

我还尝试使用常规的fill_inRSpec 方法来完全避免这种情况,但随后得到:

无法找到未禁用的字段“说明”

尽管测试被标记为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
Run Code Online (Sandbox Code Playgroud)

Fre*_*man 8

spec/rails_helper.rb

\n

需要帮助程序文件并将该模块包含在系统规范中:

\n
require "action_text/system_test_helper"\n\nRSpec.configure do |config|\n  config.include ActionText::SystemTestHelper, type: :system\nend\n\n
Run Code Online (Sandbox Code Playgroud)\n

fill_in_rich_text_area您现在可以在系统规范中使用该帮助程序:

\n
fill_in_rich_text_area "page_content", with: "Some content."\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa6 其中"page_content"是 trix 编辑器的 id。

\n

富文本区域还可以通过 placeholder 属性的值、aria-label 属性的值、其标签中的文本或其输入的名称来找到。

\n

如果页面上只有一个编辑器,则可以省略第一个参数,如下所示:

\n
fill_in_rich_text_area with: "Some content."\n
Run Code Online (Sandbox Code Playgroud)\n


小智 3

Here\xe2\x80\x99s 脚本 I\xe2\x80\x99m 使用(只需将其作为助手包含):

\n\n
def fill_in_trix_editor(id, with:)\n  find(:css, "##{id}").click.set(with)\nend\n
Run Code Online (Sandbox Code Playgroud)\n\n

ActionText 创建模型 + 属性的下划线 id。也可以检查看看。

\n