Forum: Ruby on Rails Setting Session Variables in Integration Tests

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kyle P. (Guest)
on 2009-05-07 22:26
All I want to do is set a session variable when I make a request.
Here's my code:

class ProjectIntegrationTest < ActionController::IntegrationTest
  def test_truth
    user = user_for_test
    user.access_project_index_page
  end

  def user_for_test
    open_session do |thing|
      def user.access_project_index_page
        session[:user_id] = 'someid'
        get project_index_path
        assert_response :success
      end
    end
  end
end

I'm getting this error:

test_truth(ProjectIntegrationTest):
NoMethodError: undefined method `user' for
#<ActionController::Integration::Session:0x21d7c50>
method method_missing in test_process.rb at line 464
method send! in integration.rb at line 448
method method_missing in integration.rb at line 448
method user_for_test in project_integration_test.rb at line 11

Line 11 is the line where "session[:user_id] = 'someid'" is being set.
What am I doing wrong for setting the session?

-Kyle
Frederick C. (Guest)
on 2009-05-07 23:13
(Received via mailing list)
On 7 May 2009, at 11:26, Kyle Peyton <rails-mailing-list@andreas-
s.net> wrote:

>  def user_for_test
>    open_session do |thing|
>      def user.access_project_index_page

Actually it is the above line that is raising the exception - there is
no user variable / method in scope here.
This code lois pretty strange - are you really trying to define
singleton methods on users?

Fred
Kyle P. (Guest)
on 2009-05-07 23:15
I'm pretty new to integration testing and I'm not sure how to define
tests.  I saw an example of someone doing it this way, so that's the way
I formed mine.  What is the best way to write a normal test that allows
me to set the "user_id" session variable so that the "get" can
successfully happen?

-Kyle

Frederick C. wrote:
> On 7 May 2009, at 11:26, Kyle Peyton <rails-mailing-list@andreas-
> s.net> wrote:
>
>>  def user_for_test
>>    open_session do |thing|
>>      def user.access_project_index_page
>
> Actually it is the above line that is raising the exception - there is
> no user variable / method in scope here.
> This code lois pretty strange - are you really trying to define
> singleton methods on users?
>
> Fred
This topic is locked and can not be replied to.