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
|
# shoulda [![Gem Version](https://badge.fury.io/rb/shoulda.png)](http://badge.fury.io/rb/shoulda) [![Build Status](https://secure.travis-ci.org/thoughtbot/shoulda.png)](http://travis-ci.org/thoughtbot/shoulda)
The shoulda gem is a meta gem with two dependencies:
* [shoulda-context](https://github.com/thoughtbot/shoulda-context) ([official documentation](http://rubydoc.info/github/thoughtbot/shoulda-context/master/frames))
* [shoulda-matchers](https://github.com/thoughtbot/shoulda-matchers) ([official documentation](http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/frames))
The following describes different use cases and combinations.
rspec with shoulda-matchers
---------------------------
This is what thoughtbot currently does. We write tests like:
```ruby
describe Post do
it { should belong_to(:user) }
it { should validate_presence_of(:title) }
end
```
The belong_to and validate_presence_of methods are the matchers.
All matchers are Rails 3-specific.
Add rspec-rails and shoulda-matchers to the project's Gemfile:
```ruby
group :test do
gem 'rspec-rails'
gem 'shoulda-matchers'
end
```
test/unit with shoulda
----------------------
For the folks who prefer Test::Unit, they'd write tests like:
```ruby
class UserTest < Test::Unit::TestCase
should have_many(:posts)
should_not allow_value("blah").for(:email)
end
```
The have_many and allow_value methods are the same kind of matchers
seen in the RSpec example. They come from the shoulda-matchers gem.
Add shoulda to the project's Gemfile:
```ruby
group :test do
gem 'shoulda'
end
```
test/unit with shoulda-context
------------------------------
If you're not testing a Rails project or don't want to use the matchers,
you can use shoulda-context independently to write tests like:
```ruby
class CalculatorTest < Test::Unit::TestCase
context "a calculator" do
setup do
@calculator = Calculator.new
end
should "add two numbers for the sum" do
assert_equal 4, @calculator.sum(2, 2)
end
should "multiply two numbers for the product" do
assert_equal 10, @calculator.product(2, 5)
end
end
end
```
Add shoulda-context to the project's Gemfile:
```ruby
group :test do
gem 'shoulda-context'
end
```
Credits
-------
![thoughtbot](http://thoughtbot.com/assets/tm/logo.png)
Shoulda is maintained and funded by [thoughtbot, inc](http://thoughtbot.com/community)
Thank you to all [the contributors](https://github.com/thoughtbot/shoulda/contributors)!
The names and logos for thoughtbot are trademarks of thoughtbot, inc.
License
-------
Shoulda is Copyright © 2006-2013 Tammer Saleh and thoughtbot, inc. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
|