【问题标题】:Routing Error in feature testing with RSpec使用 RSpec 进行功能测试时出现路由错误
【发布时间】:2018-09-26 08:12:20
【问题描述】:

我正在开发一个项目并尝试为其编写一些测试。目前我正在进行功能测试。
问题:我就是不能使用visit。不管我在哪里使用它。我收到错误:ActionController::RoutingError: No route matches [GET] "/"

这是一个简单的测试示例:

require 'rails_helper'

RSpec.feature "Welcome", type: :feature do
  context 'sign in user and load index' do
    visit new_user_session_path  #didn't worked with "/" nor "/login" either
  end
end

这可能是 RSpec 的问题吗?我只是不知道如何解决它。

如果有人愿意帮助我,我真的很高兴。

编辑:

routes.rb:

Rails.application.routes.draw do
  REGEX_NAME = /[^\/]+/

  constraints OperatorSubdomain do

    get '/', to: 'operators#index'

    devise_for :operators, controllers: {
      sessions: 'operators/sessions'
    }, skip: %i[registrations passwords unlocks omniauth_callbacks confirmations]

    authenticate :operator do
      require 'sidekiq/web'
      mount Sidekiq::Web => '/sidekiq'
      Sidekiq::Web.set :sessions, false
      ActiveAdmin.routes(self)
    end
  end

  constraints AppSubdomain do 
    root 'welcome#index'

  scope '/', module: 'welcome' do
    get '/faq', action: :faq
    get '/impressum', action: :imprint
  end

  get '/partners', to: 'companies#my_partners', as: :partners
  delete '/partners', to: 'companies#destroy_partnership'

  devise_scope :user do
    get  '/login' => 'users/sessions#new', as: :new_user_session
    post '/login' => 'users/sessions#create', as: :user_session

    get '/register' => 'users/registrations#new', as: :new_user_registration
    post '/register' => 'users/registrations#create', as: :user_registration

    delete '/logout', to: 'users/sessions#destroy', as: :destroy_user_session

    post '/users/notification_token', to: 'users/sessions#set_login_time', as: :notification_token

    get '/users/invitation', to: 'users#invitation', as: :users_invitation
    post '/users/invitation', to: 'users#complete_invitation'
  end

  scope '/setup', module: :setup do
    get '/', action: :index, as: :setup
    post '/', action: :create
  end

  devise_for :users, controllers: {
      omniauth_callbacks: 'users/omniauth_callbacks',
      confirmations: 'users/confirmations',
      registrations: 'users/registrations',
      passwords:     'users/passwords'
  }, skip: :session


  scope '/profiles/:name', module: :users, constraints: { name: REGEX_NAME } do
    get '/', action: :show, as: :profile
    put '/', action: :update
  end

我认为这是 routes.rb 的重要部分。

【问题讨论】:

  • 你能用routes.rb更新问题吗?
  • 好的,完成了。

标签: ruby-on-rails testing rspec routing ruby-on-rails-5


【解决方案1】:

您应该使用scenario 而不是context

RSpec.feature "Welcome", type: :feature do
  scenario 'sign in user and load index' do
    visit new_user_session_path  #didn't worked with "/" nor "/login" either
  end
end

编辑:

看起来您正在使用子域,对吗?如果是的话,你应该看看https://robots.thoughtbot.com/acceptance-tests-with-subdomains

【讨论】:

  • 啊,我明白了。但我不是在创建类似博客的东西,我只是想访问根页面。我可以将访问与子域一起使用吗?还是需要我在 spec_helper 中配置它?
  • 链接的文章展示了如何使用子域测试应用程序。不管是不是博客
  • 现在我遇到了另一个问题。我在 application.yml 中为 ENV.fetch("my_var") 声明了一个变量。如果我运行测试,它会说“my_var”没有定义。
  • 失败/错误:需要 File.expand_path('../../config/environment', FILE) 类型错误:没有将哈希隐式转换为字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
相关资源
最近更新 更多