github.com/djsun/context github.com/jeremymcanally/context
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
Original implementation by Jeremy McAnally, but heavily tweaked and borrowed from Rails Core and Pratik Naik. Repackaged as the tu-context gem by David James after pulling together various Github forks.