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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:33:32+00:00 2026-06-09T20:33:32+00:00

In rails I have a before_filter that checks and requires that the user is

  • 0

In rails I have a before_filter that checks and requires that the user is an admin for certain actions in the controller.

However, I need to write tests for these controllers.

So, I have something that looks like this:

test "should get create" do
    assert_difference('Event.count') do
      post :create, FactoryGirl.build(:event)
    end
    assert_not_nil assigns(:event)
    assert_response :success
  end 

user_factory.rb:

FactoryGirl.define do

  factory :admin do
   email 'aa@example.com'
   password 'password'
   password_confirmation 'password'
   admin true
  end
end

But need to login as an admin to be able to create the event. Any ideas on how to do this? The admin column is just a true/false column in the users table.

Edit: First Attempt:

 test "should get create" do
    admin = Factory(:admin)
    login_as(admin)
    assert_difference('Event.count') do
      post :create, FactoryGirl.build(:event)
    end
    assert_not_nil assigns(:event)
    assert_response :success
  end

generates error:

 1) Error:
test_should_get_create(EventsControllerTest):
NameError: uninitialized constant Admin

Update:

FactoryGirl.define do

  factory :user do
   email 'aa@example.com'
   password 'password'
   password_confirmation 'password'
  end
end

and

test "should get create" do
    login_as(FactoryGirl.create(:user, admin: true))
    assert_difference('Event.count') do
      post :create, FactoryGirl.build(:event)
    end
    assert_not_nil assigns(:event)
    assert_response :success
  end

and I get the error test_should_get_create(EventsControllerTest):
NoMethodError: undefined method 'login_as' for #<EventsControllerTest:0x007fb4faec1b28>

  • 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-09T20:33:33+00:00Added an answer on June 9, 2026 at 8:33 pm

    When you define your factory(:admin), FactoryGirl looks for a class called Admin, which is why you’re getting that error.

    You don’t need to create a separate factory for admin; you can simply use your User factory, passing in admin: true (that’ll override the default factory settings). So do admin = Factory(:user, admin: true). Make sure you have the user factory defined of course.

    If you want to keep the :admin factory, you need to specify that the class is User. The syntax goes something like this:factory(:admin, class: "User").

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

Sidebar

Related Questions

I have a check_user_access_control before_filter in my ApplicationController that checks the logged user's roles
I have a Rails 2.3.x app that implements the act_as_authentic in User model and
I have several controllers that require a correct user for their edit/update/delete actions. What
I have a before_filter in my Rails app that sends users to login_url if
In Rails 2.3.8 I have a credit cards controller that needs a customer_id. I
I have a before_filter in my application controller to keep a user's session alive
Using Rails v2.1, lets say you have an action for a controller that is
I have made a simple Rails application that allows people to comment on posts.
I am caching several User objects using Rails.cache.write but when I retrieve it from
I have a Rails 3.1 application that uses devise for authentication. I am trying

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.