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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:03:42+00:00 2026-06-10T23:03:42+00:00

I have a Rails app and I am trying to test it. I use

  • 0

I have a Rails app and I am trying to test it. I use devise to log in. However, I faced a problem that occurs when I want to test:

Users/ender/Projects/ratingw/spec/controllers/widgets_controller_spec.rb:4:in `block in <top (required)>': undefined local variable or method `login_user' for #<Class:0x007fe909bd5070> (NameError)

First, I want to say that I read this devise formal tutorial.

My spec_helper.rb is:

  # This file is copied to spec/ when you run 'rails generate rspec:install'
    ENV["RAILS_ENV"] ||= 'test'
    require File.expand_path("../../config/environment", __FILE__)
    require 'rspec/rails'
    require 'rspec/autorun'
    require 'capybara/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  OmniAuth.config.test_mode = true

  OmniAuth.config.mock_auth[:twitter] = {
      :provider => 'twitter',
      :uid => '123545'
      # etc.
  }

  OmniAuth.config.mock_auth[:twitter] = :invalid_credentials

  config.include Devise::TestHelpers, :type => :controller

  config.extend ControllerMacros, :type => :controller

  config.include RequestMacros, :type => :request

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

end

module ::RSpec::Core
  class ExampleGroup
    include Capybara::DSL
    include Capybara::RSpecMatchers
  end
end

and also I have a controller_macros.rb, which is located in support file:

module ControllerMacros
  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = Factory(: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

and finally, my controller_spec file is:

require 'spec_helper'

describe WidgetsController do
  login_user

  describe "User" do
    before(:each) do
      @current_user = mock_model(User, :id => 1)
      @widget = mock_model(Widget, :user_id => 1)
      Widget.stub!(:current_user).and_return(@current_user)
      Widget.stub!(:widgets).and_return(@widget)
    end

    it "should have a current_user" do
      subject.current_user.should_not be_nil
      redirect_to widgets_path
    end

    it "should not have a current_user" do
      redirect_to widgets_path new_user_session_path
    end
  end

  def mock_widget(stubs={})
    @mock_widget ||= mock_model(Widget, stubs).as_null_object
  end

  describe "Get index" do
    it "should get all widgets " do
      Widget.stub(:all) { [mock_widget] }
      get :index
      assigns(:widgets) == @widgets
    end
  end

  describe "Post widget" do
    it "creates a new widget" do
      Widget.stub(:new).with({'these' => 'params'}) { mock_widget(:save => true) }
      post :create, :widget => {'these' => 'params'}
      assigns(:widget) == @widgets
      response.should redirect_to (edit_widget_path(@mock_widget))
    end

    it "can not create a new widget" do
      Widget.stub(:new).with({'these' => 'params'}) { mock_widget(:save => false) }
      post :create, :widget => {'these' => 'params'}
      assigns(:widget) == @widgets
      redirect_to new_widget_path
    end
  end

  describe "Get widget" do
    it "shows exact widget via its uuid" do
      Widget.stub(:find).with("10") { mock_widget(:save => true) }
      get :show
      assigns(:widget) == @widget
    end
  end

  describe "Put widget" do
    it "updates the widget attributes" do
      Widget.stub(:find_by_uuid).with("6").and_return(mock_widget(:update_attributes => true))
      mock_widget.should_receive(:update_attributes).with({'these' => 'params'})
      put :update, :uuid => "6", :widget => {'these' => 'params'}
      response.should redirect_to (edit_widget_path(@mock_widget))
    end

    it "can not update the widget attributes" do
      Widget.stub(:find_by_uuid).with("6").and_return(mock_widget(:update_attributes => false))
      mock_widget.should_receive(:update_attributes).with({'these' => 'params'})
      put :update, :uuid => "6", :widget => {'these' => 'params'}
    end
  end

  describe "delete destroy" do
    it "destroys the requested widget" do
      Widget.stub(:find_by_uuid).with("10").and_return(mock_widget)
      mock_widget.should_receive(:destroy)
      get :destroy, :uuid => "10"
    end
  end
end

How can I fix this? What is the problem?

  • 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-10T23:03:44+00:00Added an answer on June 10, 2026 at 11:03 pm

    I guess the error stems from:

    config.extend ControllerMacros, :type => :controller
    

    You should have:

    config.include ControllerMacros, :type => :controller 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app I want to run on heroku to test. I
I'm trying to use RSPec to test my Ruby on Rails 3.2 app. When
I'm trying to use capybara with rspec to test my rails app. I've simplified
I have a simple Rails 3.1.rc6 app that I'm using to try to test
I have a Rails app and trying to make a remote form, so far
I have Rails 3.1.3 app and trying to plug gem redis to it. I've
I am trying to run jquery through a rails app. I have the edit
I have a Rails app that is served by Apache and Passenger, using only
Trying to do something very basic and test my Rails app on my phone.
All this weekend I have been trying to setup a Rails 3.0.4 app in

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.