【问题标题】:Capybara visit always gives me error 404Capybara 访问总是给我错误 404
【发布时间】:2016-02-12 05:26:19
【问题描述】:

我正在做一些模块化的 Sinatra 应用程序并使用 rspec 和 Capybara 进行测试。问题是我在尝试访问页面时总是收到“未找到”错误 404。当我使用命令“bundle exec rackup”并转到相同的 url 但在浏览器中一切正常。这是我的规格:

    require "spec_helper"
    require_relative '../../routes/user_routes'

    set :environment, :test


    RSpec.describe RubyPlay, type: :feature do
      include Rack::Test::Methods

      def app
        RubyPlay # this defines the active application for this test
      end

      before { visit '/' }
      it "should allow accessing the home page" do
        p page.html # prints Not found
        p page.status_code # 404
        expect(page).to have_content 'Ruby Play'
      end
    end

捆绑 exec rspec:

   RubyPlay should allow accessing the home page
   Failure/Error: expect(page).to have_content 'Ruby Play'
   expected to find text "Ruby Play" in "Not Found"

我从未在 Sinatra 中做过测试,也不知道可能出了什么问题... :)

【问题讨论】:

    标签: ruby rspec sinatra capybara http-status-code-404


    【解决方案1】:

    我找到了解决方案 - 我只需要添加:

        Capybara.app = RubyPlay
    

    到 spec_helper.rb 文件!现在它可以找到它的路线了……

    【讨论】:

    • 是的——不设置应用程序 Capybara 不知道要加载/运行什么——在包含 capybara/rails 时所有处理的 rails 中——但使用 Sinatra 你需要手动完成跨度>
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2012-09-30
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    相关资源
    最近更新 更多