sorcery icon indicating copy to clipboard operation
sorcery copied to clipboard

return value of deliver_* methods should match ActionMailer

Open alexch opened this issue 10 years ago • 2 comments

ActionMailer's deliver_ methods return an instance of Mail, which allows for easy unit testing, a la http://edgeguides.rubyonrails.org/testing.html#unit-testing :

    email = UserMailer.create_invite('[email protected]',
                                       '[email protected]', Time.now).deliver_now
    # Test the body of the sent email contains what we expect it to
    assert_equal ['[email protected]'], email.from

Unfortunately sorcery's deliver_ methods (e.g. user.deliver_reset_password_instructions!) return a class object of the user domain object, due to the use of tap inside ActiveRecordAdapter.transaction. It would be very nice if it just returned the return value of the block instead, since the block ends with a call to deliver_now and would just work.

A workaround is to turn your mail unit tests into functional tests and grab the last email off the AM queue, like this:

email = ActionMailer::Base.deliveries.last

alexch avatar Apr 24 '15 18:04 alexch

+1

harlantwood avatar Apr 24 '15 19:04 harlantwood

Hmm, I agree it could be added like this:

def deliver_password_instructions!
  mail = false
  transaction do
    generate_password
    mail = send_mail
  end
  mail
end

If you can make a PR, I'll be happy to merge it!

arnvald avatar May 03 '15 16:05 arnvald