Skip to content

Instantly share code, notes, and snippets.

@fgarcia
Created March 29, 2014 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgarcia/9854667 to your computer and use it in GitHub Desktop.
Save fgarcia/9854667 to your computer and use it in GitHub Desktop.
Padrino Helper Testing

There is great introduction here:

http://wikimatze.de/testing-helpers-in-padrino.html

However do not forget that helpers are a Sinatra feature, so you might want to search for "sinatra helper testing":

http://stackoverflow.com/a/8575684

Both links are the best way for your "spec/unit" tests. However once my own helpers evolve, I start depending on other helpers (Padrino::Helpers) and features/variables/extensions from my Padrino::Application.

Since that requires lots of mocking, I get lazy and define a "spec/integration" test using the helpers from an instance of my main application (MyWeb::App < Padrino::Application), where everything is already cooked and ready to use.

How you get access to it is a whole adventure:

  1. MyWeb::App.new seems to be the first idea, but that yields a Sinatra::Wrapper class
  2. MyWeb::App.new! can give you a straight object of your Padrino application. The Sinatra source code mentions something about avoiding the middleware (Rack??). https://github.com/sinatra/sinatra/blob/v1.3.3/lib/sinatra/base.rb#L1372-1377
  3. The generated object, provides access to all your helpers: MyWeb::App.new!.tag(:br)

Most applications has some integration/feature tests using a headless browser like Capybara. My current RSpec configuration mounts the application with this Capybara.app = Padrino.application however that is interesting because Capybara gains access to my app with a Padrino::Router, not with with a Padrino::Application < Sinatra::Base class.

TODO: Since I am not bussy learning Rails, I should make some effort studying how Rack + Rack middleweare works. After all, Padrino/Sinatra is all about exposing you to the full stack.

TODO: Explore how Capybara can pull rendered text for a http request (GET/POST...) out of Padrino::Router

Another great place to learn more about how to test helpers is looking at the tests for the Padrino::Helpers module

https://github.com/padrino/padrino-framework/tree/master/padrino-helpers/test

Just two caveats

  1. Padrino testing framework is MiniTest
  2. Padrino headless browser for testing is WebRat.

I believe those choices were quite popular in the past. These days it seems that the most popular combo is RSpec/Capybara, which is the only one I know.

Learning MiniTest/Webrat is critical to understand better Padrino code. However since I started late with Ruby programming, my day to day testing is already solved by RSpec/Capybara, that makes my learning of Padrino codebase testing a secondary thing.

@fgarcia
Copy link
Author

fgarcia commented Mar 31, 2014

To understand more about Padrino testing from any Test Framework:

http://stackoverflow.com/a/22753937/253098

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment