简体   繁体   中英

How to test a Rack::Directory serving static files with Capybara? or How to test Rack Middleware with Capybara?

I'm using Rack::Directory.new to map a directory in the public folder directly to the website and serve static files.

My config.ru contains:

map '/pdfs/' do
  run Rack::Directory.new('./public/resources/pdfs')
end
require './app/main.rb'
   run MainSinatra

I'm using a Sinatra App to run the rest of the application and using cucumber-sinatra to pre-generate some files.

When I try to load the path to the pdfs in my paths file I get an error.
features/support/paths.rb:

def path_to(page_name)
  case page_name    
  when /dias_all/
    '/pdfs'
  ...
end

Then on my steps file I put

visit dias_all

and I get a 404 error.

I tried adding this to the features/support/env.rb file but still got an error:

  class MainSinatraWorld
  ...
    include Rack::Test::Methods
    def app
      Rack::Directory.new('./public/resources/pdfs')
    end
  end

and then changed the step file visit to a get, but still got an error.

 get('/pdfs/dias')

error:

  No response yet. Request a page first. (Rack::Test::Error)

How do I test the contents of the folder through Capybara or Cucumber?

I was able to figure out how to solve this problem by changing this line in env.rb from:

Capybara.app = MainSinatra

to:

Capybara.app = eval "Rack::Builder.new {( " + File.read(File.dirname(__FILE__) +
'/../../config.ru') + "\n )}"

And this runs the application from the rack up file (config.ru) and loads all the middleware that wasn't otherwise loading.

I found the answer in this blog .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM