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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:42:30+00:00 2026-05-18T06:42:30+00:00

I am trying to test my controller’s create action. I am using Devise for

  • 0

I am trying to test my controller’s create action. I am using Devise for authentication, and have used the before_filter method to limit access to the create action to logged-in users. Here is the relevant part of my controller.

class RawDataSetsController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index, :download]

  def create
    @raw_data_set = current_user.raw_data_sets.build(params[:raw_data_set])
    if @raw_data_set.save
      redirect_to @raw_data_set
    else
      render "new"
    end
  end
end

In my test case I want to make sure that a logged-in user can create a RawDataSet. I think that I have mocked up an authenticated user according to the instructions on this blog post.

require 'spec_helper'
describe RawDataSetsController do

  include Devise::TestHelpers

  def mock_users(stubs={})
    @user ||= mock_model(User, stubs).as_null_object
  end

  def log_in_test_user
    attr = { :username => "Foobar", :email => "doineedit@foobar.com" }
    #mock up an authentication in warden as per http://www.michaelharrison.ws/weblog/?p=349
    request.env['warden'] = mock(Warden, :authenticate => mock_users(attr),
                                         :authenticate! => mock_users(attr),
                                         :authenticate? => mock_users(attr))
  end

  before do
    @rds = Factory(:raw_data_set)
  end

  describe "POST 'create'" do

    before do
      @attr = { 
        :organism_name => "Beef Jerky",
        :mass_spec_type => "My Stomach",
      }
    end

    describe "logged in user" do
      it "should create a raw_data_set when the user is logged in" do
        log_in_test_user
        lambda do
          post :create, :raw_data_set => @attr
        end.should change(RawDataSet, :count).by(1)
      end
    end
  end
end

Running this test case causes the following error:

1) RawDataSetsController POST 'create' logged in user should create a raw_data_set when the user is logged in
 Failure/Error: post :create, :raw_data_set => @attr
 undefined method `user_url' for #<RawDataSetsController:0x0000010175af88>
 # ./app/controllers/raw_data_sets_controller.rb:7:in `create'

I am baffled by this error. Upon more inspection, @raw_data_set is not an instance of the RawDataSet model class, but a user? This is what it looks like when I p @raw_data_set

#<User:0x807a06a4 @name="User_1002">

What the devil is going on? What am I doing wrong? How can I test the create action on my controller with a an authenticated user?

  • 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-18T06:42:30+00:00Added an answer on May 18, 2026 at 6:42 am

    EDIT (totally wrong first attempt removed)

    Calling as_null_object essentially tells the mock to accept all messages that aren’t stubbed and just return self. So when you call

    current_user.raw_data_sets.build(params[:raw_data_set])

    which would normally return a new RawDataSet associated to current_user, instead you get current_user again.

    So when you try to call redirect, passing in @raw_data_set, you’re passing it the mock instead of a RawDataSet instance, thus the errant call to user_url.

    I think the way to handle this is just use a real User (or a Factory) and stub out the Devise methods on it. So your mock_users becomes (for instance):

      def mock_users(stubs={})
        @user = User.create(stubs)
      end
    

    Now current_user will actually do the build and save through the association.

    Purists will tell you to mock and stub out everything until you’re blue in the face. Screw that — you’ve got better things to do. 🙂

    The other way to approach this is to test that the build message is received without checking whether the save succeeded. Presumably your model tests will verify that saving through the association works — why test again in the controller?

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

Sidebar

Related Questions

I am running on Castle's trunk, and trying to unit-test a controller-action where validation
I'm trying to test a method I have in my application, but I don't
I am trying to unit test a controller action that uses UpdateModel but I
I am trying to test that a particular method throws an expected exception from
I am trying to test a class that manages data access in the database
I'm trying to test a controller and I got this error. I understand the
I am trying to test a Controller that has a Command object with data
I am trying to test my controller and maintain separation of concerns. The first
I'm new to rails and I'm trying to test a controller with rspec. My
I am new to moq and I was trying to test a controller (MVC)

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.