Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8189087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:09:22+00:00 2026-06-07T03:09:22+00:00

In trying to test some sign-in/out functionality I want to delete some information from

  • 0

In trying to test some sign-in/out functionality I want to delete some information from the session. I found out I couldn’t access the sessions at all. I kept getting the error: undefined method `session’ for nil:NilClass.

But then to my surprise I found out I could access session from other rspec pages. Additional details are below. My question is: Why can I access session from some files and not others? And how can I make it so that I can access session in my second example below?

Details
File: spec/controllers/tenants_controller_spec.rb

require 'spec_helper'

describe TenantsController do
  specify { session[:tag].should == 'abc' }
end

File: spec/requests/test.rb

require 'spec_helper'

describe 'Test' do
  specify { session[:tag].should == 'abc' }
end

When I run the first file through rspec I get:

Failure/Error: specify { session[:tag].should == 'abc' }
  expected: "abc"
    got: nil (using ==)

Which is good. This should fail for that reason.

But, when I run the second file, I get:

 Failure/Error: specify { session[:tag].should == 'abc' }
 NoMethodError:
   undefined method `session' for nil:NilClass

So why is session an undefined method here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:09:24+00:00Added an answer on June 7, 2026 at 3:09 am

    Your 2nd test is a “request” spec which is an integration test. These are designed to simulate the browser and the helpers you get let you click buttons and fill out forms and assert text, tags on the page. It’s the wrong level of abstraction for inspecting the session object.

    If you want to stub out authentication, e.g. best to go through the app. See, e.g. here: Stubbing authentication in request spec

    Know that integration tests are “exploratory” or “smoke” tests, high level tests that check the seams between components, not the guts of the components themselves. They’re the most expensive to write and maintain. Use controller specs for verifying session stuff and move all business logic to the models where it’s easiest to test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to test out some Dapper stuff using LinqPad. Dapper needs a
Just compiled 5.3.3 from source (win32). Trying to test some header() stuff but it
I am trying to test some code. The main script requires imports from a
I'm trying to test out some XPaths using the Scrapy shell, but it seems
I'm trying to test some legacy code, using Mockito. I want to stub a
I am trying to test some of these code here http://ha.ckers.org/xss.html on my code.
I am trying to test some exceptions in my project and one of the
I am trying to learn unit testing. I am trying to unit test some
I am still trying to test my WinForm application, however some of the testing
I am new to Haskell and trying to fiddle with some test cases I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.