2

モデルにメソッドがあります

(ユーザーモデル)

  def create_reset_code
      self.attributes = {:reset_code => Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )}
      save(:validate=>false)
      UserMailer.reset_password_email(self).deliver 
  end

RSpecでテストするにはどうすればよいですか?コード生成をテストして、電子メールを送信したい

PS:Googleを使用していますが、の例は見つかりませんでした

UPD

私は2つのテストを書きます:

it "should create reset code" do           
  @user.create_reset_code
  @user.reset_code.should_not be_nil
end    

it "should send reset code by email" do           
  @user.create_reset_code

  @email_confirmation = ActionMailer::Base.deliveries.first
  @email_confirmation.to.should == [@user.email] 
  @email_confirmation.subject.should == I18n.t('emailer.pass_reset.subject')
  @email_confirmation.body.should match(/#{@user.reset_code}/)
end

しかし、これ--- @ email_confirmation.body.should match(/#{@ user.reset_code} /)----動作しません

手紙の中で私はreset_password_url(@ user.reset_code)のようにreset_codeでURLを与えます

修繕

it "should send reset code by email" do           
  @user.create_reset_code

  @email_confirmation = ActionMailer::Base.deliveries.last
  @email_confirmation.to.should == [@user.email] 
  @email_confirmation.subject.should == I18n.t('emailer.pass_reset.subject')
  @email_confirmation.html_part.body.should match /#{@user.reset_code}/
end 

仕事です!

みなさん、ありがとうございました。質問は締め切らせていただきました

4

3 に答える 3

4
it "should create reset code" do           
  @user.create_reset_code
  @user.reset_code.should_not be_nil
end   


it "should send reset code by email" do           
  @user.create_reset_code

  @email_confirmation = ActionMailer::Base.deliveries.last
  @email_confirmation.to.should == [@user.email] 
  @email_confirmation.subject.should == I18n.t('emailer.pass_reset.subject')
  @email_confirmation.html_part.body.should match /#{@user.reset_code}/
end 
于 2012-06-20T16:11:25.217 に答える
2

あなたが使用することができます

mail = ActionMailer::Base.deliveries.last

仕様でそのメソッドを呼び出した後に送信された最後の電子メールを取得するには、mail.toまたはmail.body.raw_sourceに対して仕様を実行できます。

于 2012-06-19T19:13:49.097 に答える
1

このようなものがあなたを助けるはずです..私はRspecマッチャーを使用しました

it "should test your functionality" do
user = Factory(:ur_table, :name => 'xyz', :email => 'xyz@gmail.com')
obj = Factory(:ur_model_table)
key = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join
data = obj.create_reset_code
data.reset_code.should be(key)
let(:mail) {UserMailer.reset_password_email(user) }
mail.to.should be(user.email)
end
于 2012-06-19T18:26:43.910 に答える