1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
# Test::Unit::Context
[![Build Status][0]](http://travis-ci.org/kares/test-unit-context)
Makes `Test::Unit::TestCase` 'context-able' and thus (subjectively - hopefully)
much easier to read and write. If you have ever seen RSpec than it's the very
same *context do ... end* re-invented for **Test::Unit**.
Inspired by [gem 'context'](https://github.com/jm/context) that does the same
for the good 'old' test-unit 1.2.3 bundled with Ruby 1.8.x standard libraries.
## Installation
Add it to your application's *Gemfile* (along with **test-unit**) e.g. :
group :test do
gem 'test-unit'
gem 'test-unit-context'
end
Or install it yourself, if you're not using Bundler :
$ gem install test-unit-context
## Usage
```ruby
# NOTE: do not run try running this at home!
class ChuckNorrisTest < Test::Unit::TestCase
setup do
@subject = ChuckNorris.new
end
test "can be divided by zero"
assert_equal @subject * 2, @subject / 0
end
context 'frozen' do
setup { @subject.freeze }
test "won't answer" do
assert_raise NoMemoryError do
@subject.frozen?
end
end
test "sqrt works"
assert_nothing_raised do
Math.sqrt -2
end
end
end
shared 'elementary math facts' do
test "square root is rational"
assert_kind_of Rational, Math.sqrt(@subject)
end
test "greater than infinity"
assert @infinity < @subject
end
private
setup
def create_infinity
@infinity = 1 / 0.0
end
end
uses 'elementary math facts'
context 'cloned' do
setup do
@subject = @subject.clone
end
test 'is Arnold Schwarzenegger' do
assert_instance_of Terminator, @subject
assert_nil @subject.is_a?(ChuckNorris)
end
like 'elementary math facts'
end
end
```
### Spec Mode
```ruby
require 'test/unit/context/spec'
describe ChuckNorris, '#fart' do
setup do
@subject = ChuckNorris.new
@subject.fart
end
it "creates a parallel universe" do
assert Object.const_defined?(:Universe)
assert_equal @subject, Universe.instance
assert_empty ObjectSpace.each_object(Universe).to_a
end
end
```
## Copyright
Copyright (c) 2012 [Karol Bucek](https://github.com/kares).
See LICENSE (http://www.apache.org/licenses/LICENSE-2.0) for details.
[0]: https://secure.travis-ci.org/kares/test-unit-context.png
|