context – The testing library you've been looking for
If you've ever wanted contexts in your Test::Unit tests, then context is for you. Your tests will be easier to read and write without all the magic and extra code smell!
Add contexts to Test::Unit tests
Small DSL for specifying tests that are pretty
Ability to chain context lifecycle methods (coming soon)
Add contexts using familiar syntax:
class UserTest < Test::Unit::TestCase context "A new User" do # Before/after lifecycle blocks before do @user = User.first end # Specify tests using DSL test "should have the right full_name" do assert_equal "Dude Man", @user.full_name end test "should be able to set parts of the name" do @user.first_name = "Mad" @user.last_name = "Max" @user.save assert_equal "Mad Max", @user.full_name end after do @user.first_name = "Dude" @user.last_name = "Man" @user.save! end end end
It also has aliases that match other library's syntaxes (all of which can be mixed and matched):
class UserTest < Test::Unit::TestCase context "A new Account" do test "should be new" do Account.new.new_record? end end # RSpec-esque describe "A new User" do it "should do things" do User.first.do_things! end end # Shoulda-esque context "Another User" do should "do things that are fun" do User.first.do_things!(:fun) end end end
Contexts can also be nested:
class UserTest < Test::Unit::TestCase context "A new User" do context "with clown shoes" do test "should squeak" do assert_true User.find_by_shoes("clown").squeak? end end context "without clown shoes" do test "should not squeak" do assert_false User.find_by_shoes("dressy").squeak? end end end end
You can also share behavior among contexts:
class UserTest < Test::Unit::TestCase shared "shared things" do test "things are shared" do # test logic here... end end context "the first thing" do uses "shared things" test "other things..." do # More testing... end end end
Shared behaviors can also use RSpec syntax
class UserTest < Test::Unit::TestCase share_examples_for "shared things" do it "things are shared" do # test logic here... end end describe "the first thing" do it_should_behave_like "shared things" it "other things..." do # More testing... end end end
Test::Unit (you have it; trust me)
$ gem sources -a http://gems.github.com $ sudo gem install jeremymcanally-context
Copyright © 2008 Jeremy McAnally
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original implementation by myself, but heavily tweaked and borrowed from Rails Core and Pratik Naik.