fguillen
fguillen

Reputation: 38772

Ruby, how to create a Rack::Request for testing purposes?

I have a library that take a Rack::Request and do stuff on it.

I would like to test it from an unit test and not from a functional test. So I have to create a Rack::Request instance on my own, how can I do it?

Upvotes: 10

Views: 4902

Answers (2)

Raul Murciano
Raul Murciano

Reputation: 326

Rack itself includes some unit tests for Rack::Request, you can use them as a starting point (example).

Rack::Request.new(Rack::MockRequest.env_for("http://example.com:8080/", {"REMOTE_ADDR" => "10.10.10.10"}))

Upvotes: 18

mechanicalfish
mechanicalfish

Reputation: 12826

Use Rack::MockRequest which is implemented for this purpose. See these tests for example usage.

Upvotes: 7

Related Questions