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.