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.
| 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. | | 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'.
On Sun, Jul 26, 2009 at 7:01 PM, Mike Barton<barton.mich...@gmail.com> wrote: > 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.
Are you using classic-style? What testing framework are you using?
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
> 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 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
On Jul 27, 3:49 am, Kematzy <kema...@googlemail.com> wrote:
> 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
> 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.
> On Jul 27, 2009, at 6:01 AM, Mike Barton wrote:
> > 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 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: