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 8171873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:35:57+00:00 2026-06-06T21:35:57+00:00

I’m trying to get RSpec working for a simple scaffolded app, starting with the

  • 0

I’m trying to get RSpec working for a simple scaffolded app, starting with the rspec scaffold tests.

Per the devise wiki, I have added the various devise config entries, a factory for a user and an admin, and the first things I do in my spec controller is login_admin.

Weirdest thing, though… all my specs fail UNLESS I add the following statement right after the it ... do line:

dummy=subject.current_user.inspect

(With the line, as shown below, the specs pass. Without that line, all tests fail with the assigns being nil instead of the expected value. I only happened to discover that when I was putting some puts statements to see if the current_user was being set correctly.)

So what it acts like is that dummy statement somehow ‘forces’ the current_user to be loaded or refreshed or recognized.

Can anyone explain what’s going on, and what I should be doing differently so I don’t need the dummy statement?

#specs/controllers/brokers_controller_spec.rb
describe BrokersController do
  login_admin

  def valid_attributes
    {:name => "Bill", :email => "rspec_broker@example.com", :company => "Example Inc", :community_id => 1}
  end

  def valid_session
    {}
  end

  describe "GET index" do
    it "assigns all brokers as @brokers" do
      dummy=subject.current_user.inspect    # ALL SPECS FAIL WITHOUT THIS LINE!
      broker = Broker.create! valid_attributes
      get :index, {}, valid_session
      assigns(:brokers).should eq([broker])
    end
  end
  describe "GET show" do
    it "assigns the requested broker as @broker" do
      dummy=subject.current_user.inspect  # ALL SPECS FAIL WITHOUT THIS LINE!
      broker = Broker.create! valid_attributes
      get :show, {:id => broker.to_param}, valid_session
      assigns(:broker).should eq(broker)
    end
  end

and per the devise wiki here is how I login a :user or :admin

#spec/support/controller_macros.rb
module ControllerMacros
  def login_admin
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:admin]
      sign_in Factory.create(:admin) # Using factory girl as an example
    end
  end

  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = Factory.create(:user)
      user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
      sign_in user
    end
  end
end
  • 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-06T21:35:58+00:00Added an answer on June 6, 2026 at 9:35 pm

    What a struggle! Thank you Robin, I’ve been googling on this for hours and finally saw your post; now my controller tests are working 🙂

    To add to your answer, I figured out how to get the devise session into the valid_session hash, which allows the controller tests to run properly as generated by rails.

    def valid_session
      {"warden.user.user.key" => session["warden.user.user.key"]}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

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.