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

  • SEARCH
  • Home
  • 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 6999117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:29:58+00:00 2026-05-27T20:29:58+00:00

I use Rails 3 and RSpec 2.6.0. Not sure if that’s possible, but here

  • 0

I use Rails 3 and RSpec 2.6.0.

Not sure if that’s possible, but here is what I would like to do:

describe UsersController do
  let(:user) { Fabricate :user }
  describe "GET /user/:id" do
    should_return_401_code_if_user_is_not_confirmed :get, :show, :id => user.id
  end
  describe "PUT /user/:id" do
    should_return_401_code_if_user_is_not_confirmed :put, :update, :id => user.id
  end
end

I tried to implement the macro like this:

module ControllerMacros
  def should_return_401_code_if_user_is_not_confirmed(verb, action, params = {})
    it "returns a 401 code if the user is not an admin" do
      send verb, action, params
      response.code.should == "401"
    end
  end
end

But when running those specs I get the error undefined local variable or method 'user'. I tried to switch to a @user variable defined in a before block, but it does not work either. I suspect it’s because I’m not in a example block.

Is it possible to pass to a controller macro arguments defined by let or in a before block?

Thanks!

  • 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-05-27T20:29:58+00:00Added an answer on May 27, 2026 at 8:29 pm

    From reading through this thread on the rspec-users listserve, it appears the answer is no. The issue is that your let/before vars aren’t being initialized until the it block in the body of your macro, so you can’t reference them as parameters to the macro call.

    I’m trying to do essentially the same thing in a site I’m working on. The solution I’m going with is to call the macro method with a block that will return the params hash you want, and then use instance_eval in the macro body to evaluate the block using the macro’s scope.

    #spec
    ...
    describe "GET /user/:id" do
      should_return_401_code_if_user_is_not_confirmed :get, :show { {:id => user.id} }
    end
    
    #macro
    ...
    def should_return_401_code_if_user_is_not_confirmed(verb, action, &params_block)
      it "returns a 401 code if the user is not an admin" do
        params = instance_eval &params_block
        send verb, action, params
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails RSpec file which looks something like require 'spec_helper' describe Something
I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2. I would like
I am trying to use rspec on my rails application I run spec path/to/spec/spec.rb
I'd like to use Rails 2.2.2 for one project and Rails 2.3.2 for another.
I want to use rails genrate command in gem development. Is it possible?
We are designing an application that will use Rails and Wordpress to interact with
I have a non-rails application that I want to use rails active-record migrations with.
We actually do not use test within our application (I know that's bad and
I'm using RSpec for my Rails 3 tests and trying to use Spork. I
I'm trying to use rspec with mongoid but I'm running across this error: undefined

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.