Skip to content

Commit

Permalink
Refactor rendering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Trung Lê committed Aug 26, 2015
1 parent bb9f0f4 commit 9a62eb3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
12 changes: 7 additions & 5 deletions lib/lotus/mailer/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ def self.extended(base)
end

module InstanceMethods
# @private
# @since 0.1.0
attr_accessor :mail

# Initialize a mailer
#
# @param locals [Hash] a set of objects available during the rendering process.
#
# @since 0.1.0
def initialize(locals = {})
@locals = locals
@scope = self
@mail = Mail.new
@locals = locals
@scope = self
@mail = Mail.new
end

# Render a single template with the specified format.
Expand All @@ -33,8 +37,6 @@ def render(format)
self.class.templates
self.class.templates[format].render @scope, @locals
end

attr_accessor :mail
end
end
end
Expand Down
12 changes: 3 additions & 9 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,17 @@ class ArrayMailer

class WelcomeMailer
include Lotus::Mailer

from "noreply@sender.com"
to "noreply@recipient.com"
subject "Welcome"

def greeting
"Ahoy"
end
end

class User
def initialize(name)
@name = name
end

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

class SubscriptionMailer
include Lotus::Mailer
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/templates/welcome_mailer.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
<%=greeting%>
<%= greeting %>
<p>This is a html template</p>
</body>
</html>
2 changes: 1 addition & 1 deletion test/fixtures/templates/welcome_mailer.txt.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
This is a txt template
<%=greeting%>
<%= greeting %>
42 changes: 28 additions & 14 deletions test/rendering_test.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
require 'test_helper'
require 'lotus/mailer'

describe Lotus::Mailer do
describe 'render' do
it 'renders a single template with a given format' do
InvoiceMailer.new.render(:html).must_include %(<h1>Invoice template</h1>)
LazyMailer.new.render(:html).must_include %(Hello World)
LazyMailer.new.render(:haml).must_include %(This is a haml template)
LazyMailer.new.render(:txt).must_include %(This is a txt template)
describe '#render' do
describe 'when template is explicitly declared' do
let(:mailer) { InvoiceMailer.new }

it 'renders the given template' do
mailer.render(:html).must_include %(<h1>Invoice template</h1>)
end
end

it 'renders a single template with context' do
WelcomeMailer.new.render(:html).must_include %(Ahoy)
WelcomeMailer.new.render(:txt).must_include %(Ahoy)
describe 'when template is implicitly declared' do
let(:mailer) { LazyMailer.new }

it 'looks for template with same name with inflected classname and render it' do
mailer.render(:html).must_include %(Hello World)
mailer.render(:haml).must_include %(This is a haml template)
mailer.render(:txt).must_include %(This is a txt template)
end
end

describe 'when mailer defines context' do
let(:mailer) { WelcomeMailer.new }

it 'renders template with defined context' do
mailer.render(:txt).must_include %(Ahoy)
end
end

it 'renders a single template with locals' do
luca = User.new('Luca')
mailer = RenderMailer.new(user: luca)
describe 'when locals are parsed in' do
let(:mailer) { RenderMailer.new(user: User.new('Luca')) }

mailer.render(:html).must_include %(Luca)
it 'renders template with parsed locals' do
mailer.render(:html).must_include %(Luca)
end
end
end
end

0 comments on commit 9a62eb3

Please sign in to comment.