我正在嘗試使用 rspec/capybara 測驗我的分頁功能。我使用 FactoryBot 創建了這些文章,但遇到了兩個問題。如果我創建資料:
- 在一個
before塊中,資料被保存到資料庫中,并且在套件運行后不會被擦除。 - 在場景塊內并用于
save_and_open_page查看資料是否顯示在螢屏上,它不顯示。所以,我相信資料沒有足夠的時間來保存。
寶石檔案:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.0.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.4', '>= 6.1.4.4'
gem 'react-rails'
# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false
gem "aws-sdk-s3", require: false
group :development, :test do
gem 'rspec-rails', '~> 5.0.0'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'factory_bot_rails'
gem 'lorem_ipsum_amet'
gem 'dotenv-rails'
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 4.1.0'
# Display performance information such as SQL time and flame graphs for each request in your browser.
# Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
gem 'rack-mini-profiler', '~> 2.0'
gem 'listen', '~> 3.3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
group :test do
gem 'simplecov', require: false
gem 'codecov', require: false
gem 'capybara'
gem 'webdrivers', '~> 5.0'
gem 'launchy', '~> 2.5'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
這是工廠:
FactoryBot.define do
factory :article do
sequence(:title) {|i| "Article #{i}"}
body {"This is the test body of the article."}
end
end
這是我的測驗套件。我只展示一個示例,因為其余部分的概念相同:
RSpec.feature "Paginations", type: :feature do
before(:all) do
ActionController::Base.allow_forgery_protection = true
# FactoryBot.create_list(:article, 100)
end
before(:each) do
visit articles_path
end
after(:all) do
ActionController::Base.allow_forgery_protection = false
end
scenario "user visits articles home page to see only 10 articles", js: true do
FactoryBot.create_list(:article, 10)
save_and_open_page
expect(page).to have_css "div.item-container-sizing.item-container-padding.flex-row-space-between", count: 10
end
end
uj5u.com熱心網友回復:
您的主要問題是無序執行。您在創建測驗所需的物件之前訪問該頁面,因此這些物件在呈現的頁面上不可見(在呈現頁面時它們不存在)。對于大多數測驗,您不希望visit在 before 塊中呼叫,除非您有一系列測驗都需要相同的測驗資料并訪問相同的頁面。在這種情況下,只需將 移動visit到場景中。此外,不要過度指定要查找的元素的選擇器,因為這會使您的測驗變得脆弱。您沒有顯示您的 html 的外觀,但假設此頁面僅顯示一個文章串列,則僅檢查您的一個類可能就足夠了,或者將您的期望范圍限定為頁面區域,
scenario "user visits articles home page to see only 10 articles", js: true do
FactoryBot.create_list(:article, 10)
visit articles_path
# save_and_open_page
expect(page).to have_css "div.item-container-sizing.item-container-padding.flex-row-space-between", count: 10
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/436026.html
標籤:轨道上的红宝石 红宝石 水豚 工厂机器人 rspec-rails
