遇到的问题

我在 Hello world 级别创建了一个非常简单的 Rails 应用程序,安装了 rspec-rails 并编写了以下系统规范。

require 'rails_helper'

RSpec.describe "Home", type: :system do
  before do
    driven_by(:selenium_chrome_headless)
  end

  example do
    visit root_url
    expect(page).to have_content 'Home#index'
  end
end

当你运行测试时......嗯?

$ bundle exec rspec
DEBUGGER: Attaching after process 28722 fork to child process 28745

(何も起きない)

这种状态持续了几十秒,最后以如下错误结束。

$ bundle exec rspec
DEBUGGER: Attaching after process 28722 fork to child process 28745
F

Failures:

  1) Homes 
     Failure/Error: visit root_url
     
     Net::ReadTimeout:
       Net::ReadTimeout with #<Socket:(closed)>
     
     [Screenshot Image]: /Users/jnito/dev/sandbox/without-active-record/tmp/capybara/failures_r_spec_example_groups_homes_example_at___spec_system_homes_spec_rb_8_381.png

     
     # ./spec/system/homes_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 1 minute 16.35 seconds (files took 0.5839 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/system/homes_spec.rb:8 # Homes 

顺便说一句,如果我将驱动程序更改为:rack_test,则测试通过。

   before do
-    driven_by(:selenium_chrome_headless)
+    driven_by(:rack_test)
   end
$ bundle exec rspec
.

Finished in 0.06872 seconds (files took 0.63488 seconds to load)
1 example, 0 failures

恨? ? ? ?

解决方案

visit root_url 更改为 @987654328 修复了它。只有这个! !

   example do
-    visit root_url
+    visit root_path
     expect(page).to have_content 'Home#index'
   end
$ bundle exec rspec
DEBUGGER: Attaching after process 28608 fork to child process 28634
.

Finished in 1.44 seconds (files took 0.60104 seconds to load)
1 example, 0 failures

顺便说一句,在默认状态下,root_urlroot_path 具有以下值。

puts root_url  #=> http://www.example.com/
puts root_path #=> /

我记得以前被这个迷住了,但我完全忘记了,又迷上了......?
请大家小心。

确认版本

  • 红宝石 3​​.1.2
  • 导轨 7.0.4
  • rspec-rails 6.0.0.rc1
  • 水豚 3.37.1
  • 硒-webdriver 4.4.0
  • 铬 105.0.5195.125

原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308626942.html

相关文章:

  • 2021-07-25
  • 2021-04-18
  • 2021-07-23
  • 2021-06-14
  • 2022-02-26
  • 2021-07-03
  • 2021-10-03
猜你喜欢
  • 2021-09-14
  • 2021-10-17
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
相关资源
相似解决方案