Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with "post create" in my devise controller rspec

[Ok... my first question, so be gentle.]

I am using devise for my authentication, but I have my own controller to extend what happens when the user is created. I am creating both a "user" and an "agency" at the time of the registration (sign up).

In routes...

 devise_for :users, :controllers => {:registrations => "registrations"}

My complete controller...

 class RegistrationsController < Devise::RegistrationsController
   def create
     super # creates the @user
     @agency = Agency.create! params[:agency]
     @agency.users << @user
     @agency.owner = @user
     @user.agency = @agency
     @agency.save
     @user.account_admin = true
     @user.save
   end
 end

My problem is that I want to set up an rspec to check this code. The code seems to be working, but I am shooting for 100% code coverage in my specs. Here is my entire spec...

 require 'spec_helper'
 describe RegistrationsController do
   render_views  
   describe "POST create" do
     it "creates an associated user" do
       @agency = Factory.create( :agency )
       @user = Factory.create( :user, :agency => @agency )
       User.stub(:new).with({'name' => 'pat'}) { @user }
       Agency.stub(:new).with({'name' => 'pat'}) { @agency }
       post :create, :user => {'name' => 'pat'}
       assigns(:user).should be(@user)
     end
   end
 end

But, I am getting an error on the "post create". Here is the error message

 Could not find devise mapping for path "/users?user[name]=pat"

And this is (I think) the relevant line from "rake routes"

 user_registration POST   /users(.:format)  {:action=>"create",:controller=>"registrations"}

Any thoughts?

pat

like image 886
Pat Avatar asked Apr 23 '11 17:04

Pat


1 Answers

Specifically, copying out a snippet from the link referenced by @shanethehat, the line

@request.env["devise.mapping"] = Devise.mappings[:admin]

solves the problem posed in the question. Just drop that line into a before_filter for your Devise-flavored controller test. Change :admin to the resource in question (commonly :user)

The reason that is works: Describing a SessionsController alone does not uniquely identify a specific Devise resource. For example, if your app has admin and user resources, you might like to have 2 SessionsController(s) with 2 sets of specs - one for each resource type.

In that case, for each test to operate on the right resource, you need to tell Devise which of your SessionsController(s) you mean for each set of examples. The line above does just that.

like image 180
Josh Dzielak Avatar answered Nov 14 '22 03:11

Josh Dzielak