使用 Rspec/Factory Girl/Rails 测试注册确认
Testing a signup confirmation with Rspec/Factory Girl/Rails
尝试创建一个 Rspec/Factory 女孩测试以确保涵盖 Devise 的注册确认 - 该网站有 3 种语言(日语、英语、中文)所以我想确保没有任何事情中断注册过程.
我有以下工厂:
user.rb
<< 具有一般用户邮件程序测试所需的一切
signup.rb
其中有:
FactoryGirl.define do
factory :signup do
token "fwoefurklj102939"
email "abcd@ek12o9d.com"
end
end
我要测试的设计user_mailer方法是:
def confirmation_instructions(user, token, opts={})
@user = user
set_language_user_only
mail to: @user.email,
charset: (@user.language == User::LANGUAGE_JA ? 'ISO-2022-JP' : 'UTF8')
end
我一辈子都想不出如何让 token
部分在测试中发挥作用 - 有什么建议或想法吗?
我一直在尝试这些方法(检查电子邮件是否正在发送)但没有成功:
describe UserMailer, type: :mailer do
describe "sending an email" do
after(:all) { ActionMailer::Base.deliveries.clear }
context "Japanese user emails" do
subject(:signup) { create(:signup) }
subject(:user) { create(:user) }
subject(:mail) do
UserMailer.confirmation_instructions(user, token, opts={})
end
it "sends an email successfully" do
expect { mail.deliver }.to change { ActionMailer::Base.deliveries.size }.by(1)
end
end
end
end
产生的错误是 undefined local variable or method
token'and I cannot work out why it is not coming from the
signup` 工厂。我试过改变
subject(:mail) do
UserMailer.confirmation_instructions(user, token, opts={})
end
至
subject(:mail) do
UserMailer.confirmation_instructions(user, signup.token, opts={})
end
但后来我收到了这个错误:
Failure/Error: subject(:signup) { create(:signup) }
NameError:
uninitialized constant Signup
编辑:我忘了提一些重要的事情——实际代码都适用于所有 3 种语言的用户注册,所以我确信这绝对是我在测试方面的经验不足。
subject(:mail) do
UserMailer.confirmation_instructions(user, user.confirmation_token)
end
这当然取决于您的具体实施方式,但您的用户 class 应该生成令牌:
require 'secure_random'
class User
before_create :generate_confirmation_token!
def generate_confirmation_token!
confirmation_token = SecureRandom.urlsafe_base64
end
end
创建一个单独的工厂是不必要的,并且不会起作用,因为 FactoryGirl 将尝试创建一个 Signup
的实例,我猜你没有。
工厂不是固定装置。
尝试创建一个 Rspec/Factory 女孩测试以确保涵盖 Devise 的注册确认 - 该网站有 3 种语言(日语、英语、中文)所以我想确保没有任何事情中断注册过程.
我有以下工厂:
user.rb
<< 具有一般用户邮件程序测试所需的一切
signup.rb
其中有:
FactoryGirl.define do
factory :signup do
token "fwoefurklj102939"
email "abcd@ek12o9d.com"
end
end
我要测试的设计user_mailer方法是:
def confirmation_instructions(user, token, opts={})
@user = user
set_language_user_only
mail to: @user.email,
charset: (@user.language == User::LANGUAGE_JA ? 'ISO-2022-JP' : 'UTF8')
end
我一辈子都想不出如何让 token
部分在测试中发挥作用 - 有什么建议或想法吗?
我一直在尝试这些方法(检查电子邮件是否正在发送)但没有成功:
describe UserMailer, type: :mailer do
describe "sending an email" do
after(:all) { ActionMailer::Base.deliveries.clear }
context "Japanese user emails" do
subject(:signup) { create(:signup) }
subject(:user) { create(:user) }
subject(:mail) do
UserMailer.confirmation_instructions(user, token, opts={})
end
it "sends an email successfully" do
expect { mail.deliver }.to change { ActionMailer::Base.deliveries.size }.by(1)
end
end
end
end
产生的错误是 undefined local variable or method
token'and I cannot work out why it is not coming from the
signup` 工厂。我试过改变
subject(:mail) do
UserMailer.confirmation_instructions(user, token, opts={})
end
至
subject(:mail) do
UserMailer.confirmation_instructions(user, signup.token, opts={})
end
但后来我收到了这个错误:
Failure/Error: subject(:signup) { create(:signup) }
NameError:
uninitialized constant Signup
编辑:我忘了提一些重要的事情——实际代码都适用于所有 3 种语言的用户注册,所以我确信这绝对是我在测试方面的经验不足。
subject(:mail) do
UserMailer.confirmation_instructions(user, user.confirmation_token)
end
这当然取决于您的具体实施方式,但您的用户 class 应该生成令牌:
require 'secure_random'
class User
before_create :generate_confirmation_token!
def generate_confirmation_token!
confirmation_token = SecureRandom.urlsafe_base64
end
end
创建一个单独的工厂是不必要的,并且不会起作用,因为 FactoryGirl 将尝试创建一个 Signup
的实例,我猜你没有。
工厂不是固定装置。