Mosquito, for bug-free Camping
I'm working on a simple testing framework. I'm calling it Mosquito.
It's been published as a RubyGem?
gem install mosquito
http://rubyforge.org/projects/mosquito
Currently does most of what you need for testing your app:
* Methods for doing GET, POST, PUT, DELETE * Loading fixtures * Functional tests * Unit tests * @cookies setting and access * @state hash for accessing the session!
The svn is public here:
Warning: You are Camping, not Rail-riding
Test files start with test_ (test_blog.rb). Test classes start with Test (TestBlog?).
Model and Controller test classes can both go in the same file.
A Sqlite3 :memory: database is automatically used for tests that require a database.
You can run your tests by executing the test file with Ruby or by running the autotest command with no arguments (from the ZenTest? gem).
ruby test/test_blog.rb or autotest
USAGE
Require 'mosquito' in a test case, then inherit from Camping::FunctionalTest? or Camping::UnitTest?.
A simple test looks like this:
def test_view
get '/view/1'
assert_response :success
assert_match_body %r!The quick fox jumped over the lazy dog!
end
Here is a more complete test with Functional and Unit tests:
require 'rubygems'
require 'mosquito'
require File.dirname(__FILE__) + "/../blog"
Blog.create
include Blog::Models
class TestBlog < Camping::FunctionalTest
fixtures :blog_posts, :blog_users, :blog_comments
def test_view
get '/view/1'
assert_response :success
assert_match_body %r!The quick fox jumped over the lazy dog!
end
def test_comment
assert_difference(Comment, :count, 1) {
post 'comment', :post_username => 'jim',
:post_body => 'Nice article.',
:post_id => 1
}
assert_response :redirect
assert_redirected_to '/view/1'
end
end
class TestPost < Camping::UnitTest
fixtures :blog_posts, :blog_users, :blog_comments
def test_create
post = create
assert post.valid?
end
def test_assoc
post = Post.find :first
assert_kind_of User, post.user
assert_equal 1, post.user.id
end
private
def create(options={})
Post.create({ :user_id => 1,
:title => "Title",
:body => "Body"}.merge(options))
end
end
There is also a @request variable and a @response variable that can be used to view headers, etc.
Tips
You can use assert_select in your Mosquito functional test cases:
require 'rubygems'
require 'mosquito'
require 'action_controller/assertions'
require File.dirname(__FILE__) + "/../yourapp"
YourApp.create
include YourApp::Models
class Camping::FunctionalTest
def html_document
HTML::Document.new(@response.body)
end
end
class YourApp < Camping::FunctionalTest
fixtures :yourapp_fixture
def test_list
get '/list'
assert_response :success
assert_select "ul li a", 5
end
end
This requires actionpack:
gem install actionpack
TODO
* Integration Tests * File upload testing
Any ideas or patches are appreciated.
Geoffrey Grosenbach boss@…
