Rack::Test uses last_response and last_request objects instead of Rack’s typical request and response objects. This is probably normally fine, but when you are testing functionality that requires accessing the Rack’s normal objects, they aren’t there. I found (in the comments section of this post) that you can fix this by overriding them in your test_helper.rb:
1
2
3
4
5
6
7
8
9
10
11
12
| module Test::Unit
class TestCase
include Rack::Test::Methods
...
def request(*args)
args.empty? ? last_request : rack_test_session.request(*args)
end
...
end
end |
Posted: March 26th, 2010 | Author: jay | Filed under: Code | Tags: last_request, last_response, rack, request, response, ruby, sinatra, test::unit, testing, test_helper.rb | No Comments »
I should have done this earlier. To do a number of tests, I have to repeat the same actions like create a user, log a user in, etc. I was putting these actions at the top of each test file, but have wised up and moved them to the test_helper.rb file. Here’s how that looks now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| module Test::Unit
class TestCase
...
def create_user(user_hash={})
post '/user/signup', {:user=>{:username=>"testuser",:email=>"test@test.com",:password=>"pass1",:password_confirmation=>"pass1"}.merge(user_hash)}
end
def login(user_hash={})
post "/user/login", :user=>{:username_or_email=>'testuser',:password=>'pass1'}.merge(user_hash)
end
end
end |
Posted: March 24th, 2010 | Author: jay | Filed under: Code | Tags: ruby, test, test::unit, testing, test_helper | 1 Comment »