Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set up session hash in setup for tests in Rails 3?

I have the following test that passes:

test "should create order" do
    assert_difference('Order.count') do
      post :create, { :message_text => @order.attributes }, { :var_name => 1 }
  end

Instead of adding , { :var_name => 1 } to each HTTP call, how do I add the session to the setup method?

Thanks!

like image 482
B Seven Avatar asked Jun 15 '11 18:06

B Seven


1 Answers

Inside a controller test you can access session just like you can flash.

here's a helper I use for setting the program's id

def set_current_program(program=programs(:direct_debit))
  @current_program = program
  session[:program_id] = @current_program && @current_program.id
end

or in the setup, you could do

def setup
  session[:var_name] = 1
end

then just do your test as usual

test "should create order" do
  assert_difference('Order.count') do
    post :create, :message_text => @order.attributes
  end
end

You can even assert the value of session, if the action should have changed it

assert_equal 24, session[:something_changed]
like image 177
Matthew Rudy Avatar answered Nov 15 '22 07:11

Matthew Rudy