使用RSPEC/Factory Girl/Rails测试注册确认



尝试创建RSPEC/Factory Girl测试以确保涵盖Deeise在注册上的确认 - 该网站有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令牌' and I cannot work out why it is not coming from the Ingip`工厂。我尝试更改

subject(:mail) do
  UserMailer.confirmation_instructions(user, token, opts={})
end

to

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

当然,这取决于您的确切实现是什么,但是您的用户类应生成令牌:

require 'secure_random'
class User
  before_create :generate_confirmation_token!
  def generate_confirmation_token!
    confirmation_token = SecureRandom.urlsafe_base64
  end
end

创建一个单独的工厂是不需要的,并且由于FactoryGirl会尝试创建Signup实例,因此我猜您没有。

工厂不是固定装置。

最新更新