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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:46:44+00:00 2026-06-17T21:46:44+00:00

I’ve got some funny behavior when trying to get Devise to sign in properly

  • 0

I’ve got some funny behavior when trying to get Devise to sign in properly in my controller testing. It seems to work in certain cases, but not in others. I’m not sure if this is an interaction between Devise and FactoryGirl or something else at work.

First off, here’s my factories:

factory :advisor do
  name "Jason Jones"
  association :user
  initialize_with {Advisor.find_or_create_by_name('Jason Jones')}
end

factory :client do
  name "Rich Homeowner"
  association :advisor
end

factory :user do
    email "jason@jones.com"
    password "testpassword"
    initialize_with {User.find_or_create_by_email('jason@jones.com')}
end

my controller:

class ClientsController < ApplicationController
  before_filter :authenticate_user!

 def destroy
    @client = current_user.advisor.clients.where(:id => params[:id]).first

    @client.destroy
    flash[:notice] = 'Client deleted.'
    redirect_to clients_path
  end

and my controller test:

 describe "DELETE destroy" do
    it "should delete a client" do

        a = FactoryGirl.create(:advisor)
        c = FactoryGirl.create(:client, :advisor => a)
        login_user(a.user)

        expect{
          delete :destroy, :id => c.id
          response.should be_redirect
          assigns(:client).should eq(c)
        }.to change(Client, :count).by(-1)

      end
  end

the login_user spec helper is where it gets funky. if I uncomment the line below, forcing the user to be set to the FactoryGirl object, the test passes. If I leave it commented, Devise attempts to sign in as the passed user (which I have verified via debugging is the same user in the DB), but it does not actually sign in. The sign_in call actually returns the same array in both cases, but based on following the execution path, the controller code is never executed, because Devise redirects to the login page.

def login_user(user=nil)
   @request.env["devise.mapping"] = Devise.mappings[:user]


   if user.nil?
     user = FactoryGirl.create(:user)
   end

   # user = FactoryGirl.create(:user) # uncommenting this line causes test to pass

   sign_in user
 end

How do I get the sign_in to work properly?

For the record, when it comes to TDD for Rails, I spend 10 min getting my actual code to work properly and 2 hours jumping through hoops to get my test code to do what it’s supposed to.

  • 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-17T21:46:46+00:00Added an answer on June 17, 2026 at 9:46 pm

    I recently started trying to swallow the philosophy of TDD and admit that I had the same exact feeling as you did at first. Your time estimates seem pretty accurate, 10 minutes of development and 2 hours of test case implementation. My first advice is, like many things in life, it gets better. The first time you make a seemingly innocuous change then realize that you’ve broken half your test regression you’ll be glad you took the pill.

    With that said, this is going to sound like a cop out because you’re asking why Devise doesn’t work and my answer is: you shouldn’t care. Clearly you’re doing something wrong, and I’m afraid I can’t tell what it is from the information given, but I think I can help anyway.

    The only thing I see wrong above is that your spec is testing at least four things:

    1. Response is a redirect.
    2. @client is assigned.
    3. A Client is destroyed.
    4. Devise is providing proper authentication.

    A spec should test one thing and one thing only. As tempting as it may be to test more, I don’t recommend it. Cucumber or other integration tests test bunches of things, but not specs.

    Devise is not something you should test here, so stub it out. I think something like this will work:

    before :each do
      @advisor = FactoryGirl.create(:advisor)
      controller.stub(:authenticate_user!).and_return(true)
      controller.stub(:current_user).and_return(@advisor.user)
    end
    

    After this, create three different it "should" do blocks for the three things you’re testing.

    Another tip is I don’t think you need to specify association when defining a FactoryGirl factory. I think this is only needed for polymorphic associations. Normally you can just give the name of the association without a value and it will run the factory with the same name. Just watch out for infinite loops.

    I hope that helps.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.