ruby-on-railsruby-test

ruby session variables


I'm trying to write basic assert test:

def assert_session_has ( sessionvar )
    return assert_not_nil session[:sessionvar]
end

when I compile:

def test_auth_bob
    #some setup and other validation methods
    assert_not_nil session[user]
    #more validations...
end

I get the following error:

test_auth_bob(UserControllerTest):
NameError: undefined local variable or method `user' for #<UserControllerTest:0x3460c28>
/test/functional/user_controller_test.rb:23:in `test_auth_bob'

Any ideas?


Solution

  • You lost a colon. As painful as that sounds, some people don't even notice.

    def test_auth_bob
        #some setup and other validation methods
        assert_not_nil session[:user]
        #more validations...
    end
    

    Without the : user refers to a variable or method, with a colon user is the symbol :user. This post on the internet about symbols appears to explain more… but I have not read it. I'm being called away from the computer by a thrilling but slow moving episode of The Wire. It's good.