How to configure 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 a session to the installation method?

Thanks!

+6
source share
1 answer

Inside the controller test, you can access session just like you can flash .

here is the helper that I use to set the program id

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

or in setup you can do

 def setup session[:var_name] = 1 end 

then just run the test as usual

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

You can even claim a session value if the action was to change it

 assert_equal 24, session[:something_changed] 
+11
source

Source: https://habr.com/ru/post/890632/


All Articles