i create a customized devise registration controller and i want to test it with rspec.
I've tried it with a very simple test :
it "creates a new parent" do
  Parent.should receive(:new)
  post :create
end
but i get this exception:
Failures:
  1) Parent::RegistrationsController POST create creates a new parent
     Failure/Error: post :create, { :commit => "Daftar",
     uncaught throw `warden'
     # /home/starqle/.rvm/gems/ree-1.8.7-2010.02/gems/devise-1.1.3/lib/devise/hooks/timeoutable.rb:16:in `throw'
     # /home/starqle/.rvm/gems/ree-1.8.7-2010.02/gems/devise-1.1.3/lib/devise/hooks/timeoutable.rb:16
I already put this line within my test:
describe Parent::RegistrationsController do
  include Devise::TestHelpers
end
I also already put this line:
request.env["devise_mapping"] = Devise.mappings[:parent]
anybody have ideas to solve this problem?
My previous answer is a little confusing. sorry.
Updated answer: root cause is user is not "confirmed" before "sign in".
@user.confirm! 
sign_in @user
then everything is fine.
I am fresher in ruby. I am using rails 3 with devise and factory girl.
I was searching for how to authenticate user for rspec.
I was stucked at before_filter: authenticate_user! in controller.
Finally I got solution (thanks to Siwei Shen) What I am doing is
2.
require 'spec_helper'
describe StudentsController do
  before(:each) do
    @user = Factory.create(:user)  #:user from factory girl with admin privilages
    @request.env['devise.mapping'] = :user
    @user.confirm!
    sign_in @user
  end
  it "can get index of student" do
    get :index
    response.should be_suclogin_as @user
  end
  it "can create student" do
    #in student model : validates :name, :presence=> true 
    post :create, :student => {name => "student1" } 
    answer = Student.find_by_name("student1")
    answer.name.should == "student1"
  end
end
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With