Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8939 (closed defect: worksforme)

Opened 3 years ago

Last modified 2 years ago

Flash not accessible before request in tests (session seems to be an array instead of a hash)

Reported by: umjames Assigned to: core
Priority: normal Milestone: 1.x
Component: ActionPack Version: edge
Severity: normal Keywords: session functional flash response Array TypeError
Cc: Zargony

Description

When trying to access or assign to the session in a functional test, I get a TypeError saying it cannot convert the session key to an Integer. When I print the class of the session, it returns "Array".

Here's a simple test case that illustrates this (this is from a fresh new Rails 1.2.3 project with nothing more than a test controller generated for it):

require File.dirname(__FILE__) + '/../test_helper'
require 'test_controller'

# Re-raise errors caught by the controller.
class TestController; def rescue_action(e) raise e end; end

class TestControllerTest < Test::Unit::TestCase
  def setup
    @controller = TestController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  def test_session
  	puts session.class
  	puts @response.session.class
  	
  	session["test_user"] = "testuser1"
  end
end

When I run this test, I get the following:

Loaded suite test_controller_test
Started
Array
Array
E
Finished in 0.047 seconds.

  1) Error:
test_session(TestControllerTest):
TypeError: can't convert String into Integer
    test_controller_test.rb:18:in `[]='
    test_controller_test.rb:18:in `test_session'

1 tests, 0 assertions, 0 failures, 1 errors

Attachments

flash_access_before_request_in_tests_fix.patch (1.1 kB) - added by Zargony on 09/21/07 14:44:45.
TestResponse patch and unit test

Change History

07/10/07 22:22:05 changed by manfred

  • severity changed from major to normal.
  • milestone changed from 1.2 regressions to 1.x.

Setting milestone to 1.x because this is not a regression from an earlier version.

07/14/07 22:41:55 changed by lifofifo

  • status changed from new to closed.
  • resolution set to incomplete.

Please attach tests/patch. Check PatchRequirements for more details.

09/21/07 14:41:25 changed by Zargony

  • status changed from closed to reopened.
  • cc set to Zargony.
  • summary changed from Session in functional tests seems to be an array instead of a hash to Flash not accessible before request in tests (session seems to be an array instead of a hash).
  • version changed from 1.2.3 to edge.
  • keywords changed from session functional to session functional flash response Array TypeError.
  • resolution deleted.

I faced a similar problem today that seems to be related to the same problem: @request.session is not initialized to a TestSession and therefore the flash is not accessible before doing the actual get/post test-request (I posted the long story in my Blog).

09/21/07 14:44:45 changed by Zargony

  • attachment flash_access_before_request_in_tests_fix.patch added.

TestResponse patch and unit test

09/21/07 15:56:28 changed by Zargony

The above patch fixes the problem that flash[] is not accessible in a test, but storing something into the flash before doing a request, does store it in response.session and therefore the tested controller (which accesses request.session) does not see it.

09/23/07 21:39:50 changed by mpalmer

  • status changed from reopened to closed.
  • resolution set to worksforme.

This isn't a bug, surely? You just use the third and fourth arguments to get/post/etc to set the session and flash for the request.

09/24/07 11:30:45 changed by Zargony

Thanks for pointing this out. I didn't know you can set the flash with a 4th parameter to get/post/etc. Intuitively, I thought that I have to set it by accessing the flash hash before doing a request.