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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:11:39+00:00 2026-06-16T03:11:39+00:00

I am new to rails and rspec. I have a custom method in my

  • 0

I am new to rails and rspec. I have a custom method in my controller which is not an action. I am trying to test this method using spec.

Here is my controller method:

def find_target_by_id(target_id)    
    begin
      @target = Target.find(target_id)
    rescue ActiveRecord::RecordNotFound
      flash[:error] = "Target with Id #{target_id} does not exist."
      redirect_to root_url
    end
end

Here is my rspec test for this method:

context "is given invalid id" do
   before do
     Target.stub(:find).with("1").and_raise(ActiveRecord::RecordNotFound)
   end
   it "returns flash[:error] " do
     TargetsController.new.find_target_by_id("1")
     flash[:error].should eq("Target with Id 1 does not exist.")
   end

   it "redirects to root url " do
      TargetsController.new.find_target_by_id("1")
      response.should redirect_to(root_url)
   end
end

However, when run the test, I get the error:

Failure/Error: TargetsController.new.find_target_by_id("1").should have(flash[:error])
 RuntimeError:
   ActionController::Base#flash delegated to request.flash, but request is nil: #<TargetsController:0x007fced708ae50 @_routes=nil, @_action_has_layout=true, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil>
 # ./app/controllers/targets_controller.rb:71:in `find_target_by_id'
 # ./spec/controllers/targets_controller_spec.rb:212:in `block (4 levels) in <top (required)>'

Any help is much appreciated.

  • 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-16T03:11:41+00:00Added an answer on June 16, 2026 at 3:11 am

    You can’t access the flash in Rspec unless an actual web request has been made. That’s what the error is suggesting – it’s looking at request.flash, but request is nil, as you haven’t made a web request.

    A few ideas:

    • Make a GET (or POST or whatever) request in your test to an action that calls this method so you can actually have access to the flash
    • Don’t set the flash in your helper method, but instead return an error message or raise an exception and leave the flash message setting to your controller action

    If I was in this situation, I’d take the second approach. Leave the controllery stuff (like setting the flash and whatnot) to the controller actions and keep your helper methods simple and straightforward. It’ll definitely help keep your tests simpler.

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

Sidebar

Related Questions

I'm new to rails and I'm trying to test a controller with rspec. My
Rails 3, RSpec 2: In my rspec test for a controller, I'm using post
I'm a bit new to Rails/RSpec/Capybara, so this is probably a newbie question, but
I'm not new to Rails or Rspec, but I'm new to making gems. When
I'm brand new to Ruby/Rails/RSpec, and hopefully this is a dumb/simple question. I'm currently
Working on a new Rails 3.2.9 app with rspec and capybara. I have the
I'm pretty new to Rails. I have been developing my app with RSpec. I
I am using cucumber and RSpec for my new ruby on rails application and
I'm trying to test for the presence of a form. I'm new to Rails.
I am new Rspec and just started by generating a new controller on Rails

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.