Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec-2 and Devise

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?

like image 925
giosakti Avatar asked Feb 27 '23 02:02

giosakti


2 Answers

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.

like image 129
Siwei Avatar answered Mar 07 '23 12:03

Siwei


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

  1. include TestHelpers in spec/spec_helper.rb

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
like image 31
Swapnil Chincholkar Avatar answered Mar 07 '23 12:03

Swapnil Chincholkar