Testing with Melee and Capybara
The idea that Steak had about acceptance tests is great. You don’t necessarily have to use a solution like Cucumber to write acceptance test for your application. You could just as easily use rspec or another testing suite instead. Being the riot fan that I am, I wanted to have something similiar for the Riot community so that we too can write beautiful acceptance tests.
Introducing Melee
Melee is to Riot what Steak is to Rspec. It’s a minimalist approach to acceptance testing. It keeps close to how you write tests but give you a simple DSL to give you that ‘acceptance test’ feel. For a side to side comparison, this is what a melee test looks like:
feature %Q{
To write better software
as a ruby developer and a riot user
I need to write me some acceptance test
} do
background { "that i'm no different than setup" }
scenario "With some background, it should really be no different than a setup" do
topic =~ /setup/
end
given(:a_string) { "yay for acceptance test" }
scenario "With some string that I have, it should ensure that the string" do
a_string
end.matches %r{acceptance} # Takes the same macros
end
The same test in plain riot would be:
context %Q{
To write better software
as a ruby developer and a riot user
I need to write me some acceptance test
} do
setup { "that i'm no different than setup" }
asserts "With some background, it should really be no different than a setup" do
topic =~ /setup/
end
helper(:a_string) { "yay for acceptance test" }
asserts "With some string that I have, it should ensure that the string" do
a_string
end.matches %r{acceptance} # Takes the same macros
end
Integration with Capybara
Ok, that’s fine and dandy, but what about actual integration with Capybara? Good question. Despite that capybara comes with easy integration with rspec, there is an easy workaround provided to allow you to use it in any test suite.
We’ll use the auto-generated test_config.rb from padrino as a guide.
All you need to do is require and include like so:
## test_config.rb
# ...
require 'capybara/dsl' # require the Capybara DSL
class Riot::Situation
include Rack::Test::Methods
include Capybara
# ...
end
Now, you can use capybara like you would. Here’s an example:
require File.expand_path('../../../test_config', __FILE__)
feature %{
When I access the link pages
I should see a form
that allows me to shorten urls
} do
background do
Capybara.app = Shortener
visit '/links/new'
end
scenario "With the links page, I should see a text field" do
find_field('Url').visible?
end
scenario "With the links page, I should be able to submit a url" do
fill_in 'Url', :with => 'http://www.google.com'
click_button 'Submit'
current_path
end.matches %r{links\/show}
end
There we have it. Riot now has acceptance tests ready to use with a library like Capybara. To see some demostration of melee/capybara intergration, you can check out a Padrino URL shortener demo I made to demonstrate some of the features of both padrino and riot.
happy Testing, and Happy Father’s Day!
-
xchele likes this
-
arthurchiu posted this