A Sinatra extension and repository of common RSpec helpers when testing your Sinatra apps/gems.
# Add Gemcutter to your RubyGems sources $ gem sources -a http://gemcutter.com $ (sudo)? gem install sinatra-tests
This Gem depends upon the following:
-
sinatra ( >= 0.10.1 )
-
rspec (>= 1.3.0 )
-
rack-test (>= 0.5.3)
-
rspec_hpricot_matchers (>= 0.1.0)
In your spec/spec_helper.rb file add the following:
require 'sinatra/tests'
By just requiring that gem, you essentially get all of this:
require 'sinatra/base' require 'test/unit' require 'rack/test' require 'spec' require 'spec/interop/test' require 'rspec_hpricot_matchers'
and a few other goodies.
Declare the following Spec block:
Spec::Runner.configure do |config| config.include RspecHpricotMatchers config.include Sinatra::Tests::TestCase config.include Sinatra::Tests::RSpec::SharedSpecs end
In your TestApp block, register the Sinatra::Tests extension, by adding the following:
class MyTestApp < Sinatra::Base register(Sinatra::Tests) <snip...> end
And finally define the TestCase block like this:
class Test::Unit::TestCase Sinatra::Base.set :environment, :test end
That’s all. Now onto the niceties this provides you.
A simple matcher that tests for an even number
1.should_not be_even 2.should be_even
A simple matcher that tests for a <head> with a <title> tag with the given text.
body.should have_a_page_title("Home | Site Title") body.should have_a_page_title(/Home/)
A simple matcher that tests for <h(1..6)> header tag with the given text.
NB! Throws an Exception when there’s more than one page header on the page.
body.should have_a_page_header('Page Header') body.should have_a_page_header(/Contact Information/, 'h1') body.should have_a_page_header(/Contact Information/, 'body > h1')
A simple matcher that tests for a <a href>
tag with class ‘ui-btn :action-link’ and a number of other specific values, derived from the other arguments passed.
body.should have_a_ui_btn('div', :edit, :article, 'Edit') => expects <a href="/articles/:id/edit" class="ui-btn edit-link" title="edit article">Edit</a> body.should have_a_ui_btn('div', :delete, :article) => => expects <a href="/articles/:id" class="ui-btn delete-link" title="delete article">DELETE</a>
A simple matcher that tests for a <a href>
tag with class ‘ui-btn edit-link’
body.should have_an_edit_btn('td.actions', :post) body.should have_an_edit_btn('td.actions', :post, 'Custom Btn Text')
A simple matcher that tests for a <a href>
tag with class ‘ui-btn delete-link’
body.should have_a_delete_btn('td.actions', :post) body.should have_a_delete_btn('td.actions', :post, 'Custom Btn Text')
A simple matcher that tests for a <a href>
tag with class ‘ui-btn show-link’
body.should have_a_show_btn('td.actions', :post) body.should have_a_show_btn('td.actions', :post, 'Custom Btn Text')
These are just some of the shared specs that I have been using, but they are being changed at the moment, so no guarantees of them surviving or remaining in their current form / functionality.
Dump a list of methods for the current app
it_should_behave_like "debug => app.methods"
Tests the body output for a <debug>
tag, ie: prints out the entire body content
it_should_behave_like "debug"
Checks that we got a 200 status (OK), and content-type is text/html
it_should_behave_like "HTTP headers" it_should_behave_like "HTML"
Checks that we got a 200 status (OK), and content-type is text/css
it_should_behave_like "CSS"
Checks that the page has a <div id="main-content"></div>
it_should_behave_like "div#main-content"
Checks that the page has an <h2>
tag within the <div id="main-content"></div>
it_should_behave_like "div#main-content > h2"
More to be addded later…
Checks that the page has a form with a <input type="hidden"...>
tag.
it_should_behave_like "forms > faux method > input.hidden"
More to be addded later…
For a better understanding of this Gem, make sure you study the ‘sinatra-tests/spec/*_spec.rb
’ files.
If something is not behaving intuitively, it is a bug, and should be reported. Report it here: github.com/kematzy/sinatra-tests/issues
-
Add more tests to various matchers.
-
Create a test suite for the Shared Specs.
-
Add Test::Unit (assert*) matchers and shared tests. (Please fork and add if you want this!)
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history.
-
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
-
Send me a pull request. Bonus points for topic branches.
Copyright © 2009 Kematzy [ kematzy gmail com ]
Released under the MIT license.