Created
April 15, 2011 12:02
-
-
Save dchelimsky/921590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Test | |
module Unit | |
TestCase = RSpec::Core::ExampleGroup | |
end | |
end | |
class Test::Unit::TestCase | |
def self.inherited(host) | |
host.set_it_up host.name.gsub(/(Spec|Test)/,'') | |
def host.method_added(name) | |
if name.to_s =~ /^test_/ | |
specify name do | |
send name | |
end | |
end | |
super | |
end | |
host.register | |
end | |
end | |
RSpec.configure do |config| | |
config.expect_with :stdlib | |
end | |
# With the above hax + standard RSpec configuration options, we now | |
# have test/unit syntax. | |
class TestFive < Test::Unit::TestCase | |
def test_it_equals_five | |
assert_equal 5,5 | |
end | |
end | |
# Now that syntax is out of the way, here are some things you get from RSpec | |
# that you don't get from Test/Unit | |
# | |
################################# | |
# Readable output | |
# | |
# $ rspec example_spec.rb -fd | |
# | |
# Five | |
# test_it_equals_five | |
# | |
# Finished in 0.0006 seconds | |
# 1 example, 0 failures | |
# | |
################################# | |
# Pending examples | |
# | |
# class TestFive < Test::Unit::TestCase | |
# def test_it_equals_five | |
# pending | |
# assert_equal 5,5 | |
# end | |
# end | |
# $ rspec example_spec.rb -fd | |
# | |
# Five | |
# test_it_equals_five (PENDING: No reason given) | |
# | |
# Pending: | |
# Five test_it_equals_five | |
# # No reason given | |
# # ./example_spec.rb:12 | |
# | |
# Finished in 0.00052 seconds | |
# 1 example, 0 failures, 1 pending | |
# | |
################################# | |
# Pending examples that tell you when they don't need to be pending any longer | |
# because the block content passes (AFAIK, Rails' implementation of pending | |
# doesn't support this): | |
# | |
# class TestFive < Test::Unit::TestCase | |
# def test_it_equals_five | |
# pending do | |
# assert_equal 5,5 | |
# end | |
# end | |
# end | |
# | |
# $ rspec example_spec.rb -cfd | |
# | |
# Five | |
# test_it_equals_five (FAILED - 1) | |
# | |
# Failures: | |
# | |
# 1) Five test_it_equals_five FIXED | |
# Expected pending 'No reason given' to fail. No Error was raised. | |
# # ./example_spec.rb:32:in `test_it_equals_five' | |
# # ./example_spec.rb:13:in `send' | |
# # ./example_spec.rb:13:in `method_added' | |
# | |
# Finished in 0.00054 seconds | |
# 1 example, 1 failure | |
# | |
## Run one test method by its line number: | |
# | |
# $ rspec example_spec.rb:30 | |
# Run filtered using {:line_number=>30} | |
# . | |
# | |
# Finished in 0.00043 seconds | |
# 1 example, 0 failures | |
# | |
################################# | |
# Profile mode: lists the top 10 slowest examples | |
# | |
# $ rspec example_spec.rb -p | |
# . | |
# | |
# Top 1 slowest examples: | |
# Five test_it_equals_five | |
# 0.00016 seconds ./example_spec.rb:12 | |
# | |
# Finished in 0.00038 seconds | |
# 1 example, 0 failures | |
# | |
# And much, much more. And all of this is without even using any of RSpec's syntax. | |
# If you add this bit of configuration: | |
RSpec.configure do |c| | |
c.alias_example_to :test | |
end | |
# Now you can use strings for test names, just like Rails does! But, what | |
# neither Test/Unit nor Rails t/u extensions let you do is add metadata to the | |
# examples: | |
class TestTaggingExamples < Test::Unit::TestCase | |
test "example that runs fast", :fast => true do | |
# ... | |
end | |
test "example that runs slow", :slow => true do | |
# ... | |
end | |
end | |
# Now you can run the fast examples like this: | |
# | |
# $ be rspec example_spec.rb --tag fast --format doc | |
# Run filtered using {:fast=>true} | |
# | |
# TaggingExamples | |
# example that runs fast | |
# | |
# Finished in 0.00037 seconds | |
# 1 example, 0 failures | |
# | |
# ... and you can run the slow examples like this: | |
# | |
# $ rspec example_spec.rb --tag slow --format doc | |
# Run filtered using {:slow=>true} | |
# | |
# TaggingExamples | |
# example that runs slow | |
# | |
# Finished in 0.0004 seconds | |
# 1 example, 0 failures | |
# | |
# The tag names are arbitrary and you can use them to organize examples any way | |
# you like, and run subsets of them at will. | |
# | |
################################# | |
# | |
# The above is just the tip of the iceberg, but hopefully helps to show that | |
# there is more to RSpec than its syntax. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love it! :)