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>'
背景
expect
方法,例如。 expect(page).to have_current_path(payment_success_url)
,它将使用Capybara的等待行为(因此,在页面加载之前测试不会超时)。但是,当我这样做时,会收到上面的错误。[expect
是Capybara还是RSpec的一部分,我有点困惑。它在Capybara的文档中,因此我认为它应该
just work
,但我目前仍处于工作状态。相关源代码# 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: "[email protected]"
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
使用的详细信息和版本
expect
的测试运行正常)require "application_system_test_case"
require "minitest/autorun" # new line
require "rspec/expectations/minitest_integration" # new line
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: "[email protected]"
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