簡體   English   中英

如何使用 RSpec 測試 ActionText?

[英]How to test ActionText using RSpec?

我正在嘗試編寫一個 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

我還嘗試創建一個 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

spec/rails_helper.rb

需要幫助文件並將模塊包含在系統規范中:

require "action_text/system_test_helper"

RSpec.configure do |config|
  config.include ActionText::SystemTestHelper, type: :system
end

您現在可以在系統規范中使用fill_in_rich_text_area助手:

fill_in_rich_text_area "page_content", with: "Some content."

...其中"page_content"是trix-editor的id。

富文本區域也可以通過占位符屬性的值、aria-label 屬性的值、來自其標簽的文本或通過其輸入的名稱找到。

如果頁面上只有一個編輯器,則可以省略第一個參數,如下所示:

fill_in_rich_text_area with: "Some content."

這是我正在使用的腳本(只需將其包含為幫助程序):

def fill_in_trix_editor(id, with:)
  find(:css, "##{id}").click.set(with)
end

ActionText 創建模型 + 屬性的下划線 id。 您也可以檢查以查看。

我不是 100% 確定,但似乎ActionText::SystemTestHelper可能尚未發布到 Rails 的“穩定”版本中。 它可能會與下一個一起提供(或者您可能正在運行一個尚不包含它的舊版本)。

您可以嘗試以下幾種方法:

  • 更新 Rails 版本,看看是否有幫助
  • 使用來自 github 的當前master Rails 版本(不推薦)
  • 自己添加模塊作為規范的幫助模塊,然后在需要的地方自己包含它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM