我在哪里确认与factory - girl创建的用户?

时间:2022-03-29 23:25:41

Using rails, devise, rspec & factorygirl:

使用rails, devices, 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.

尝试为我的站点创建一些测试。我正在使用可确认模型进行设计,所以当我使用FactoryGirl创建用户时,用户没有被确认。

This is my factories.rb:

这是我factories.rb:

FactoryGirl.define do
  factory :user do
    full_name             "Aren Admin"
    email                 "aren@example.com"
    password              "arenaren"
    password_confirmation "arenaren"
    role_id               ADMIN
  end
end

And this is my rspec test file:

这是我的rspec测试文件:

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.

我得到一个错误,因为用户没有被确认。通过搜索,我确信我需要使用“确认”方法!它属于工厂。rb文件,但我不确定把它放在哪里。

7 个解决方案

#1


9  

Try user.confirm! in your before block

user.confirm试试!在你的街区

found here

在这里找到

#2


32  

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

还可以将confirmed_at属性设置为如下所示。工作对我来说:

FactoryGirl.define do
  factory :user do
    full_name             "Aren Admin"
    email                 "aren@example.com"
    password              "arenaren"
    password_confirmation "arenaren"
    role_id               ADMIN
    confirmed_at          Time.now
  end
end

#3


23  

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

更好的是,执行以下操作(那么您不需要为每个测试套件创建before过滤器)

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

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

在这里找到:https://*.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

编辑01/27再次更新语法

FactoryGirl.define do
  #Other factory definitions

  factory :confirmed_user, :parent => :user do
    after(:create) { |user| user.confirm! }
  end
end

#4


1  

Adding this line inside the factory definition worked for me

在工厂定义中添加这一行对我很有用

before(:create) {|user| user.skip_confirmation! }

之前(创建){ |用户| user.skip_confirmation !}

#5


1  

Put the Devise confirmable logic in the after(:build) callback...

在after(:build)回调中放置可验证的设计逻辑……

FactoryGirl.define do
  factory :user do
    after(:build) do |u|
      u.confirm!
      u.skip_confirmation_notification!
    end
 ...
end

For me, putting confirm! or skip_confirmation! in the after(:create) block caused validation errors on the email parameter and did not work.

对我来说,把确认!或skip_confirmation !在after(:create)块中,导致电子邮件参数上的验证错误,无法工作。

#6


1  

You should call skip_confirmation! before create so this is persisted on the user.

你应该叫skip_confirmation !在创建之前,这是对用户持久的。

before(:create) do |user|
  user.skip_confirmation!
end

#7


1  

This is the factory that worked for me

这就是为我工作的工厂

FactoryGirl.define do
  factory :user do
    sequence :email do |n|
      "address#{n}@example.com"
    end
    sequence :password do |n|
      "password#{n}"
    end

    factory :confirmed_user do
      before(:create) {|user| user.skip_confirmation! }
    end
  end
end

#1


9  

Try user.confirm! in your before block

user.confirm试试!在你的街区

found here

在这里找到

#2


32  

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

还可以将confirmed_at属性设置为如下所示。工作对我来说:

FactoryGirl.define do
  factory :user do
    full_name             "Aren Admin"
    email                 "aren@example.com"
    password              "arenaren"
    password_confirmation "arenaren"
    role_id               ADMIN
    confirmed_at          Time.now
  end
end

#3


23  

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

更好的是,执行以下操作(那么您不需要为每个测试套件创建before过滤器)

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

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

在这里找到:https://*.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

编辑01/27再次更新语法

FactoryGirl.define do
  #Other factory definitions

  factory :confirmed_user, :parent => :user do
    after(:create) { |user| user.confirm! }
  end
end

#4


1  

Adding this line inside the factory definition worked for me

在工厂定义中添加这一行对我很有用

before(:create) {|user| user.skip_confirmation! }

之前(创建){ |用户| user.skip_confirmation !}

#5


1  

Put the Devise confirmable logic in the after(:build) callback...

在after(:build)回调中放置可验证的设计逻辑……

FactoryGirl.define do
  factory :user do
    after(:build) do |u|
      u.confirm!
      u.skip_confirmation_notification!
    end
 ...
end

For me, putting confirm! or skip_confirmation! in the after(:create) block caused validation errors on the email parameter and did not work.

对我来说,把确认!或skip_confirmation !在after(:create)块中,导致电子邮件参数上的验证错误,无法工作。

#6


1  

You should call skip_confirmation! before create so this is persisted on the user.

你应该叫skip_confirmation !在创建之前,这是对用户持久的。

before(:create) do |user|
  user.skip_confirmation!
end

#7


1  

This is the factory that worked for me

这就是为我工作的工厂

FactoryGirl.define do
  factory :user do
    sequence :email do |n|
      "address#{n}@example.com"
    end
    sequence :password do |n|
      "password#{n}"
    end

    factory :confirmed_user do
      before(:create) {|user| user.skip_confirmation! }
    end
  end
end