Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 302
fguillen Avatar asked Jan 10 '14 15:01

fguillen


People also ask

How do I run a test case in Ruby?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

How do you test a method in Ruby?

Most methods can be tested by saying, “When I pass in argument X, I expect return value Y.” This one isn't so straightforward though. This is more like “When the user sees output X and then enters value V, expect subsequent output O.” Instead of accepting arguments, this method gets its value from user input.

What is a rack test?

Rack testing is a process that tests a construction sealant on specific performance aspects including: Weathering while under compression.

How to test controllers in Rails?

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response. Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework.


2 Answers

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"}))
like image 167
Raul Murciano Avatar answered Oct 20 '22 00:10

Raul Murciano


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

like image 34
mechanicalfish Avatar answered Oct 20 '22 01:10

mechanicalfish