Brian
Brian

Reputation: 6071

Rails simulate application instance / request

I am following along with the following:

http://railscasts.com/episodes/151-rack-middleware

I'm trying to test the middleware using rspec.

I'm not sure the best approach to this.

I am trying to get at the request headers to see if the request is from a mobile device.

It seems like I would somehow need to pass in an application instance, but would also need to simulate request headers?

Please advise.

Upvotes: 0

Views: 156

Answers (1)

Leo Correa
Leo Correa

Reputation: 19789

In order to test middleware with Rspec you need to define a method that makes an instance of your middleware either by doing

let(:inner_app) { lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['All good!'] } }


let(:app) { SomeMiddleware.new }

or

def app
  SomeMiddleware.new
end

You'll also have to include the module Rack::Test::Methods in either your spec_helper.rb or your test file.

Then any request you do in Rspec will go through that middleware.

Check this resource out http://blog.kesor.net/2012/06/05/rack-middleware/

Upvotes: 1

Related Questions