Last active
December 2, 2024 09:35
-
-
Save wbotelhos/4f5e3a1161fb5d77ada301085e6a4d82 to your computer and use it in GitHub Desktop.
How to Set Session on Rails Request Spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
Rails.application.routes.draw do | |
if Rails.env.test? | |
namespace :test do | |
resource :session, only: :create | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set_request_session(variables) | |
post test_session_path, params: { variables: variables } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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