Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I confirm user created with FactoryGirl?

Using rails, devise, rspec & factorygirl:

Trying to create some tests for my site. I'm using the confirmable model for devise so when I create a user using FactoryGirl, the user isn't confirmed.

This is my factories.rb:

FactoryGirl.define do
  factory :user do
    full_name             "Aren Admin"
    email                 "[email protected]"
    password              "arenaren"
    password_confirmation "arenaren"
    role_id               ADMIN
  end
end

And this is my rspec test file:

require 'spec_helper'

describe "Admin pages" do

  subject { page }

  describe "home page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit admin_home_path }

    it { should have_content("#{ROLE_TYPES[user.role_id]}") }
  end
end

I'm getting an error because the user is not confirmed. By searching around I'm pretty sure I need to use the method 'confirm!' and that it belongs in the factories.rb file, but I'm not sure where to put it.

like image 952
Kevin K Avatar asked Aug 25 '12 19:08

Kevin K


2 Answers

You could also set the confirmed_at attribute as follows. Works for me:

FactoryGirl.define do
  factory :user do
    full_name             "Aren Admin"
    email                 "[email protected]"
    password              "arenaren"
    password_confirmation "arenaren"
    role_id               ADMIN
    confirmed_at          Time.now
  end
end
like image 121
auralbee Avatar answered Nov 11 '22 07:11

auralbee


Better yet, do the following (then you don't need to create a before filter for every test suite)

Factory.define :confirmed_user, :parent => :user do |f|
  f.after_create { |user| user.confirm! }
end

found here: https://stackoverflow.com/a/4770075/1153149

Edit to add non-deprecated syntax

FactoryGirl.define do |f|
  #Other factory definitions

  factory :confirmed_user, :parent => :user do
    after_create { |user| user.confirm! }
  end
end

Edit 01/27 To Update Syntax Again

FactoryGirl.define do
  #Other factory definitions

  factory :confirmed_user, :parent => :user do
    after(:create) { |user| user.confirm! }
  end
end
like image 24
Joeyjoejoejr Avatar answered Nov 11 '22 07:11

Joeyjoejoejr