Testing Sinatra helper methods

417 views
Skip to first unread message

Mike Barton

unread,
Jul 26, 2009, 6:01:47 PM7/26/09
to sinatrarb
Hi,

Is there a way to test the methods defined in the 'helper' block
within the sinatra application?
I'm using Rack::Test and following the sinatra documentation on the
sinatrarb site, but I can seem to find anything about testing the
helper methods.

Thanks

Mike

Christer Nilsson

unread,
Jul 26, 2009, 6:53:52 PM7/26/09
to sina...@googlegroups.com
| Mike
I've asked this question before, and the answer was: Use black box testing. That is, just simulate get,put,post and delete and check the results using e.g. Hpricot.
 
Personally, I sometimes prefer deeper testing.
I've accomplished that by defining get '/test'.
 
hope this helps
 
/Christer

Damian Janowski

unread,
Jul 26, 2009, 7:31:22 PM7/26/09
to sina...@googlegroups.com

Are you using classic-style?
What testing framework are you using?

Kematzy

unread,
Jul 26, 2009, 10:49:59 PM7/26/09
to sina...@googlegroups.com

Mike,

I was struggling with this issue as well before, but think I've solved
it now.

In my spec_helper.rb (using RSpec) I create a test app:

class MyTestApp < MyApp # the name of your app or S::B

get '/tests' do
case params[:engine]
when 'erb'
erb(params[:view], :layout => params[:layout] )
when 'haml'
haml(params[:view], :layout => params[:layout] )
else
params.inspect
end
end

end

Then the Test::Unit::TestCase declaration looks like this: (also in
spec_helper.rb)

class Test::Unit::TestCase
include Rack::Test::Methods

# I declare the app value in before blocks in each actual spec file,
# to prevent confusions between multiple apps (ie: testing with
options on/off)
# def app
# MyTestApp.new
# end

def setup
Sinatra::Base.set :environment, :test
end

def erb_app(view, options = {})
options = {:layout => '<%= yield %>', :url => '/
tests' }.merge(options)
get options[:url], :view => view, :layout =>
options[:layout], :engine => :erb
end

def haml_app(view, options = {})
options = {:layout => '= yield ', :url => '/
tests' }.merge(options)
get options[:url], :view => view, :layout =>
options[:layout], :engine => :haml
end

end


Finally, in my tests I just do this:

it "should return ...." do
erb_app '<%= name_of_my_helper_method %>'
last_response.body.should == 'expected result'
end


This works quite well I would say, and I only wish I had thought about
it earlier.

Mike Barton

unread,
Jul 28, 2009, 6:08:59 PM7/28/09
to sinatrarb
Thanks for your replies everyone.
I've been using rspec with rack::test for TDD on my models etc.
From your replies it looks I should switch to cucumber and webrat to
black box test the expected HTML.

Thanks again

Ben Lovell

unread,
Aug 2, 2009, 6:02:00 AM8/2/09
to sina...@googlegroups.com
2009/7/28 Mike Barton <barton....@gmail.com>

Thanks for your replies everyone.
I've been using rspec with rack::test for TDD on my models etc.
From your replies it looks I should switch to cucumber and webrat to
black box test the expected HTML.

Thanks again

May I just add, there is a fantastic series of blog posts where this dude evolves his sinatra/couchdb app through both cukes/webrat and rspec here:


Definitely worth a look for some pointers.

Cheers,
B
Reply all
Reply to author
Forward
0 new messages