How to test with RSpec if an email is delivered

后端 未结 8 1080
清酒与你
清酒与你 2020-12-25 11:09

I\'d like to test if an email is delivered if I call a controller method with :post. I\'ll use email_spec so I tried this snipped here: http://rubydoc.info/gems/email_spec/1

相关标签:
8条回答
  • 2020-12-25 11:45

    To add a little more, make sure if you're going to stub out a call using should_receive that you have an integration test elsewhere testing that you're actually calling the method correctly.

    I've been bit a few times by changing a method that was tested elsewhere with should_receive and having tests still pass when the method call was broken.

    If you prefer to test the outcome rather than using should_receive, shoulda has a nice matcher that works like the following:

    it { should have_sent_email.with_subject(/is spam$/) }
    

    Shoulda documentation

    More information on using Shoulda Matchers with rSpec

    0 讨论(0)
  • 2020-12-25 11:46

    Try email-spec

    describe "POST /signup (#signup)" do
      it "should deliver the signup email" do
        # expect
        expect(UserMailer).to(receive(:deliver_signup).with("email@example.com", "Jimmy Bean"))
        # when
        post :signup, "Email" => "email@example.com", "Name" => "Jimmy Bean"
      end
    end
    

    more examples here: https://github.com/email-spec/email-spec#testing-in-isolation

    0 讨论(0)
提交回复
热议问题