Skip to content

Instantly share code, notes, and snippets.

@wbotelhos
Last active June 2, 2023 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wbotelhos/4f5e3a1161fb5d77ada301085e6a4d82 to your computer and use it in GitHub Desktop.
Save wbotelhos/4f5e3a1161fb5d77ada301085e6a4d82 to your computer and use it in GitHub Desktop.
How to Set Session on Rails Request Spec
# frozen_string_literal: true
Rails.application.routes.draw do
if Rails.env.test?
namespace :test do
resource :session, only: :create
end
end
end
def set_request_session(variables)
post test_session_path, params: { variables: variables }
end
# frozen_string_literal: true
module Test
class SessionsController < ApplicationController
def create
variables = params.permit(variables: {})
variables[:variables].each { |variable, value| session[variable] = value }
head(:created)
end
end
end
# frozen_string_literal: true
require "spec_helper"
RSpec.describe Test::SessionsController, type: :request do
it "sets values on session" do
post test_session_path, params: { variables: { variable_1: "value_1", variable_2: "value_2" } }
expect(response).to have_http_status(:created)
session[:variable_1] = "value_1"
session[:variable_2] = "value_2"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment