Skip to content

Commit

Permalink
Raise error when from/to delivery data is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Sep 23, 2015
1 parent 6106e13 commit a2729ab
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lib/lotus/mailer.rb
Expand Up @@ -8,6 +8,12 @@

module Lotus
module Mailer
class MissingDeliveryDataError < ::StandardError
def initialize
super("Missing delivery data, please check 'from', 'to' and 'subject'")
end
end

DEFAULT_TEMPLATE = :txt.freeze

CONTENT_TYPES = {
Expand Down Expand Up @@ -134,6 +140,8 @@ module InstanceMethods
# @since 0.1.0
def deliver
mail.deliver
rescue ArgumentError
raise MissingDeliveryDataError
end
end

Expand Down
24 changes: 16 additions & 8 deletions test/delivery_test.rb
Expand Up @@ -6,6 +6,22 @@
Lotus::Mailer.deliveries.clear
end

it 'can deliver with specified charset' do
CharsetMailer.deliver(charset: charset = 'iso-2022-jp')

mail = Lotus::Mailer.deliveries.first
mail.charset.must_equal charset
mail.parts.first.charset.must_equal charset
end

it "raises error when 'from' isn't specified" do
-> { MissingFromMailer.deliver }.must_raise Lotus::Mailer::MissingDeliveryDataError
end

it "raises error when 'to' isn't specified" do
-> { MissingToMailer.deliver }.must_raise Lotus::Mailer::MissingDeliveryDataError
end

describe 'test delivery with hardcoded values' do
before do
WelcomeMailer.deliver
Expand Down Expand Up @@ -102,14 +118,6 @@
body.must_include %(<h1>Hello World!</h1>)
body.wont_include %(This is a txt template)
end

it 'can deliver with specified charset' do
CharsetMailer.deliver(charset: charset = 'iso-2022-jp')

mail = Lotus::Mailer.deliveries.first
mail.charset.must_equal charset
mail.parts.first.charset.must_equal charset
end
end

# describe 'custom delivery' do
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures.rb
Expand Up @@ -19,6 +19,22 @@ class CharsetMailer
subject 'こんにちは'
end

class MissingFromMailer
include Lotus::Mailer
template 'missing'

to "recipient@example.com"
subject "Hello"
end

class MissingToMailer
include Lotus::Mailer
template 'missing'

from "sender@example.com"
subject "Hello"
end

class User < Struct.new(:name, :email); end

class LazyMailer
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/templates/missing.txt.erb
@@ -0,0 +1 @@
Missin'

0 comments on commit a2729ab

Please sign in to comment.