【问题标题】:Check a route does not exists in Rails 4检查Rails 4中不存在路线
【发布时间】:2013-06-27 07:53:49
【问题描述】:

我停用了用户注册 (Gem Devise),我想进行测试以确保路由 /users/sign_up 不存在。

为此,我在 spec/features/user_spec.rb 中创建了一个测试

require 'spec_helper'
require 'capybara/rspec'

feature "Users" do
  scenario "could not register " do
    expect(:get => "/users/sign_up").not_to be_routable
  end
end

当我运行这个测试时,我有这个错误:

1) Users could not register
   Failure/Error: expect(:get => "/users/sign_up").not_to be_routable
   NoMethodError:
     undefined method `routable?' for {:get=>"/users/sign_up"}:Hash
   # ./spec/features/user_spec.rb:8:in `block (2 levels) in <top (required)>'

【问题讨论】:

    标签: rspec capybara ruby-on-rails-4


    【解决方案1】:

    遇到类似问题。解决办法是:

    .1 创建spec/routing/users_routing_spec.rb

    .2 在那个文件里面写:

    require "spec_helper"
    
    describe UsersController do
      describe "routing" do
    
        it "routes to #index" do
          expect(:get => "/users/sign_up").not_to be_routable
        end
    end
    

    【讨论】:

      【解决方案2】:

      来自here

      be_routeable 匹配器最好与 should_not 一起使用,以指定一个 给定的路由不应该是可路由的。 在路由规范中可用 (在规范/路由中)和控制器规范(在规范/控制器中)。

      在 Capybara 功能中,您可以执行以下操作:

      scenario "could not register " do
        visit("/user/sign_up")
        expect(page.status_code).to be(404)
      end
      

      【讨论】:

      • 我确实将我的测试移到了控制器中,它可以工作。 visit("/user/sign_up") 抛出 No route matches [GET] "/user/sign_up" 获取信息。感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多