DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on β€’ Edited on

1

making complex test data in Capybara

πŸ¦„ First, Notes

This technique is not actually only for the Capybara testing, but also for RSpec testing. However, the main use case is Capybara.

πŸ€” Situation

You need to prepare complex test data that is for multiple tests.

It seems to be too complex for Factorybot. But, It seems to be easy with UI manipulations.

πŸ‘ Solution

You can use a module and include it, call it from before {} block.

#rails_helper.rb
Dir[Rails.root.join('spec/rails_helpers/**/*.rb')].each { |f| require f }
Enter fullscreen mode Exit fullscreen mode
# spec/rails_helpers/book_operation_scripts.rb
module BookOperationScripts
  def create_published_book(author, options = {})
    click_on('publish a new book')
    fill_in 'v2_book[author]', with: options.fetch(:name, 'n350071')
    fill_in 'v2_book[title]', with: options.fetch(:name, 'enjoy ruby')
    fill_in 'v2_book[price]', with: '8'
    # ...
    page.execute_script("$('form#publish-form').submit()")
  end
end
Enter fullscreen mode Exit fullscreen mode
# spec/v2/feature/books_spec.rb
require 'rails_helper'

feature 'book/', js: true do
  include BookOperationScripts
  let(:author){ create(:v2_author) }
  before do
    create_v2_published_book(author)
  end
end
Enter fullscreen mode Exit fullscreen mode

πŸ”— Parent Note

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more