| Path: | README.txt |
| Last Update: | Mon Sep 24 11:53:32 PDT 2007 |
A testing helper for those times when you go Camping. Apply on the face, neck, and any exposed areas such as your Models and Controllers. Scrub gently, observe the results.
Make a few files and directories like this:
public/
blog.rb
test/
test_blog.rb
fixtures/
blog_comments.yml
blog_posts.yml
blog_users.yml
Setup test_blog.rb like this:
require 'rubygems'
require 'mosquito'
require File.dirname(__FILE__) + "/../public/blog"
Blog.create
include Blog::Models
class TestBlog < Camping::WebTest
fixtures :blog_posts, :blog_users, :blog_comments
def setup
super
# Do other stuff here
end
test "should get index" do
get
assert_response :success
assert_match_body %r!>blog<!
end
test "should get view" do
get '/view/1'
assert_response :success
assert_kind_of Article, @assigns[:article]
assert_match_body %r!The quick fox jumped over the lazy dog!
end
test "should change profile" do
@request['SERVER_NAME'] = 'jonh.blogs.net'
post '/change-profile', :new_photo => upload("picture.jpg")
assert_response :success
assert_match_body %r!The pic has been uploaded!
end
end
# A unit test
class TestPost < Camping::ModelTest
fixtures :blog_posts, :blog_users, :blog_comments
test "should create" do
post = Post.create( :user_id => 1,
:title => "Title",
:body => "Body")
assert post.valid?
end
test "should be associated with User" do
post = Post.find :first
assert_kind_of User, post.user
assert_equal 1, post.user.id
end
end
You can also use old-school methods like def test_create, but we think this way is much more natural.
Mosquito includes Jay Fields’ dust gem for the nice test method which allows more descriptive test names and has the added benefit of detecting those times when you try to write two tests with the same name. Ruby will otherwise silently overwrite duplicate test names without warning, which can give a false sense of security.
Inherit from Camping::WebTest or Camping::ModelTest. If you define setup, be sure to call super so the parent class can do its thing.
You should also call the MyApp.create method if you have one, yourself. You will also need to include MyApp::Models at the top of your test file if you want to use Models in your assertions directly (without going through MyApp::Models::SomeModel).
Make fixtures in test/fixtures. Remember that Camping models use the name of the mount plus the model name: blog_posts for the Post model.
See blog_test.rb for an example of both Web and Model tests.
Mosquito is one file, just like your app (right?), so feel free to ship it included with the app itself to simplify testing.
These directives are highly recommended when using Mosquito:
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
Do you prefer RSpec syntax? You can get halfway there by putting this include in your test file:
require 'spec/test_case_adapter'
Then you can use should and should_not on objects inside your tests.
Geoffrey Grosenbach topfunky.com, with a supporting act from the little fairy julik.nl and the evil multipart generator conceived by maxidoors.ru.