📜  rails rspec 测试电子邮件已发送 - Ruby 代码示例

📅  最后修改于: 2022-03-11 15:04:50.005000             🧑  作者: Mango

代码示例1
describe "User request update" do
  let(:user_one) { FactoryGirl.create(:user) }
  let(:user_two) { FactoryGirl.create(:user) }
  # let(:mail) { UserMailer.notify_users(user_one, user_two) } 

  # If your method sends two separate emails:
  it "#notify_users should notify both users" do
    UserMailer.notify_users(user_one, user_two)
    expect(ActionMailer::Base.deliveries.count).to eq(2)
    expect(ActionMailer::Base.deliveries[0].to).to include(user_one.email)
    expect(ActionMailer::Base.deliveries[1].to).to include(user_two.email)
  end

  # If your method sends a single email with both recipients
  it "#notify_users should notify both users" do
    UserMailer.notify_users(user_one, user_two)
    expect(ActionMailer::Base.deliveries.count).to eq(1)
    expect(ActionMailer::Base.deliveries.last.to).to include(user_one.email)
    expect(ActionMailer::Base.deliveries.last.to).to include(user_two.email)
  end
end