当手动构建记录时,为什么此工厂导致测试失败?

时间:2016-01-27 16:47:11

标签: ruby-on-rails rspec factory-bot

在Rails应用程序中,我正在测试用户确认

scenario "unconfirmed user cannot login" do
  user = create(:user, confirmation_sent_at: 10.days.ago ) #FactoryGirl factory
  login user
  expect(current_path).to eq(#unconfirmed_user_path)
end

此操作失败,并且用户已登录。

但是,如果我按如下方式更改第一行

user = create(:user)
user.confirmation_sent_at = 10.days.ago
user.save
login user .....

测试通过。

我在俯瞰什么?这两段代码是等价的不是吗?

修改

#factories/user_factory.rb

factory :user do
  email { Faker::Internet.email }
  password Faker::Internet.password(10, 20) 
end

# models/user.rb
# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  email                  :string(255)      default(""), not null
#  encrypted_password     :string(255)      default(""), not null
#  confirmation_token     :string(255)
#  confirmed_at           :datetime
#  confirmation_sent_at   :datetime
#  ...

class User < ActiveRecord::Base

  devise :invitable, :database_authenticatable, :registerable, :confirmable, :lockable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable

 ...
end 

0 个答案:

没有答案