【问题标题】:RSpec - Capybara and Puma - Routing ErrorRSpec - Capybara 和 Puma - 路由错误
【发布时间】:2018-05-18 21:30:31
【问题描述】:

我正在尝试将 Capybara 用作前端测试套件 (Vue),但我从 Puma 收到以下错误:

Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"

     ActionController::RoutingError:
       No route matches [GET] "/login"

这是什么原因,我该如何解决?是不是跟配置有关?让我知道是否应该在我的问题中包含更多代码。

rails_helper.rb:

ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?

require 'spec_helper'
require 'rspec/rails'
require "pundit/rspec"
require "capybara/rails"
require "capybara/rspec"
require "capybara/poltergeist"
require 'database_cleaner'
require 'factory_girl'
require 'shoulda/matchers'
require "paperclip/matchers"
require 'support/spec_helpers/warden_controller_helpers'
require 'webmock/rspec'
require 'support/spec_helpers/webmock_helper'


Capybara.javascript_driver = :poltergeist

login_spec.rb:

require 'rails_helper'

RSpec.feature "User login", type: :feature, js: true do

  before :each do
      @user = User.create!(
        first_name: "Bob",
        last_name: "Brown",
        email: "bob@email.com",
        password: "bob1",
        has_accepted_terms: true
      )
    end

  scenario "User logs in with correct credentials" do
    visit root_path
    save_screenshot
    click_on 'Log In'
    sleep 1
    save_screenshot

  end

end

【问题讨论】:

  • 需要更多信息以进行故障排除。什么是创建“/登录”路线?是设计吗?我们可以看看你的路线文件吗?当您在本地浏览器中尝试时,这是否有效?
  • 将运行rails routes 的输出添加到您的问题中。
  • 我在 routes.rb 中没有 /login 路由,我使用 vue 路由器来渲染 Login.vue 组件。我是否使用适当的库来测试我的情况?现在一切都在浏览器中运行。
  • 是否所有的 vue JS 文件都被转译和填充以兼容 Capybara.javascript_driver = :selenium_chrome_headless 作为起点,直到您需要自定义驱动程序注册)
  • @ThomasWalpole 我刚刚尝试使用以下方法进行设置:drivy.engineering/running-capybara-headless-chrome 我仍然遇到同样的错误,我应该注意哪些细微差别?

标签: ruby-on-rails vue.js rspec capybara puma


【解决方案1】:

因此,如果我运行开发服务器并转到 'localhost:3000(这基本上就是您告诉 Capybara 做的事情),那么页面上的“登录”链接会损坏,指向 app.localhost:3000/login应用程序不处理。这是因为访问没有 app 子域的 URL 使用不包含 JS 的 application.html.haml 布局(因此没有 Vue 路由器来处理 /login 路径)。但是,如果我在浏览器中转到app.localhost:3000,那么会有一个带有有效Log In 链接的页面,因为所有JS 都加载在该页面上(包括Vue 路由器),因为它使用app.html.haml 布局(BaseController)

您需要修复主页链接才能正常运行和/或配置 Capybara 以默认连接到 app 子域

Capybara.app_host = 'http://app.localhost' # hostname with ‘app’ sub domain that resolves to interface the AUT is being run on
Capybara.always_include_port = true 

基本上,Capybaras 会告诉您您的应用程序主页已损坏,因为它已损坏。

【讨论】:

  • 我知道在访问主页时我需要子域app,但现在我尝试呈现的每个页面在 URL 中都有一个额外的app(即 app.app.localhost:62281 /login),这导致我的 ajax 调用中断。是否可以让Capybara只在首页添加app
  • @jj008 如果应用程序在 Capybara 请求时在页面上创建类似的 URL,那么它也会为普通用户执行此操作,听起来您确实需要弄清楚您的路由和 URL生成并修复它。不过,要回答您的问题,如果您删除上面的 Capybara.app_host 设置,您可以随时调用 visit('http://app.localhost/') 而不是 visit root_path 如果需要,但我猜您以后仍然会遇到 URL 问题,因为您当前url 创建逻辑听起来很糟糕。也许在测试环境中检查config.action_dispatch.tld_length 是否正确。
猜你喜欢
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多