【发布时间】:2020-04-27 18:28:54
【问题描述】:
当我使用 rails test:system 运行系统测试时,我收到以下错误:
Error:
PaymentSessionsTest#test_payment_succesful_if_valid_details_provided:
NoMethodError: undefined method `expect' for #<PaymentSessionsTest:0x00007fffd82485d0>
Did you mean? exec
test/system/payment_sessions_test.rb:26:in `block in <class:PaymentSessionsTest>'
背景
我正在为 rails 编写我的第一个系统测试,并且我的页面需要一段时间才能加载。从Capybara documentation 开始,它建议使用expect 方法,例如。 expect(page).to have_current_path(payment_success_url) 将使用 Capybara 的等待行为(因此在页面加载之前测试不会超时)。但是,当我这样做时,我会收到上述错误。
我有点困惑expect 是 Capybara 还是 RSpec 的一部分。它在 Capybara 的文档中,所以我认为它应该正常工作,但我目前被卡住了。
相关源码
# test\system\payment_sessions_test.rb
require "application_system_test_case"
class PaymentSessionsTest < ApplicationSystemTestCase
setup do
@event = events(:one)
end
test "payment successful if valid details provided" do
# Enter payment details on invite page
visit event_url(@event)
fill_in "Your name", with: "John Doe"
fill_in "Email", with: "test@test.com"
fill_in "Gift amount", with: "20"
click_on "Pay"
# Stripe Checkout
fill_in "cardNumber", with: "4242424242424242"
fill_in "cardExpiry", with: "01#{Date.today.next_year.strftime("%y")}" # grab year always as next year in 2 digit format
fill_in "cardCvc", with: "123"
fill_in "Name on card", with: "Mr John Doe"
fill_in "billingPostalCode", with: "N1 7GU"
click_on "Pay"
# Payment success page
expect(page).to have_current_path(payment_success_url) # ensure Capybara waits for page to load after clicking Pay
assert_selector "h1", text: "Payment successful!"
end
end
# test\application_system_test_case.rb
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 900]
# driven_by :selenium, using: :headless_chrome
def setup
# ensure url helpers use correct host and port for system tests
Rails.application.routes.default_url_options[:host] = Capybara.current_session.server.host
Rails.application.routes.default_url_options[:port] = Capybara.current_session.server.port
end
end
# test\test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'bcrypt' # required for devise (used when creating encrypted password for user fixtures)
require 'pry'
class ActiveSupport::TestCase
include HashidsHelper
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
include Devise::Test::IntegrationHelpers # include devise test helpers in all tests
setup do
# set hashid for each event record
# NB. can't set using fixtures as event.id is dynamic
FixMissingHashidsService.run
end
end
# Gemfile
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
# gem 'chromedriver-helper'
# Using chromedriver in Windows, added to path, via WSL1
end
使用的细节和版本
- Rails 5.2.3
- 通过 WSL (1) 运行并使用 Windows chromedriver 二进制文件(Chrome 打开并运行测试 - 其他不使用
expect的测试运行良好) - 在测试中使用 Stripe Checkout 测试网关(即,它实际上转到 checkout.stripe.com/pay/cs_test_etc...)作为测试流程的一部分
【问题讨论】:
标签: ruby-on-rails ruby capybara