Capybara访问总是给我错误404

时间:2016-02-11 20:22:43

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

我正在做一些模块化的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做过测试,也不知道可能出现什么问题...... :)

1 个答案:

答案 0 :(得分:1)

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

    Capybara.app = RubyPlay

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