Skip to content

Commit

Permalink
use github-flavored-markup in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Dec 4, 2011
1 parent 1354f1a commit d13d28f
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Gemfile-custom.sample
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
group :development do group :development do
gem 'interactive_rspec' gem 'interactive_rspec'
gem 'yard' gem 'yard', "~> 0.7.4"
gem "relish", "~> 0.5.0" gem "relish", "~> 0.5.0"
gem "guard-rspec", "0.5.0" gem "guard-rspec", "0.5.0"
gem "growl", "1.0.3" gem "growl", "1.0.3"
Expand Down
198 changes: 112 additions & 86 deletions README.md
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,12 @@
# rspec-core # rspec-core


```
gem install rspec # for rspec-core, rspec-expectations, rspec-mocks
gem install rspec-core # for rspec-core only
```

# overview

rspec-core provides the structure for writing executable examples of how rspec-core provides the structure for writing executable examples of how
your code should behave. It uses the words "describe" and "it" so we can your code should behave. It uses the words "describe" and "it" so we can
express concepts like a conversation: express concepts like a conversation:
Expand All @@ -9,19 +16,21 @@ express concepts like a conversation:


## basic structure ## basic structure


describe Order do ```ruby
it "sums the prices of its line items" do describe Order do
order = Order.new it "sums the prices of its line items" do
order.add_entry(LineItem.new(:item => Item.new( order = Order.new
:price => Money.new(1.11, :USD) order.add_entry(LineItem.new(:item => Item.new(
) :price => Money.new(1.11, :USD)
order.add_entry(LineItem.new(:item => Item.new( )))
:price => Money.new(2.22, :USD), order.add_entry(LineItem.new(:item => Item.new(
:quantity => 2 :price => Money.new(2.22, :USD),
) :quantity => 2
order.total.should eq(Money.new(5.55, :USD)) )))
end order.total.should eq(Money.new(5.55, :USD))
end end
end
```


The `describe` method creates an [ExampleGroup](../RSpec/Core/ExampleGroup). Within the The `describe` method creates an [ExampleGroup](../RSpec/Core/ExampleGroup). Within the
block passed to `describe` you can declare examples using the `it` method. block passed to `describe` you can declare examples using the `it` method.
Expand All @@ -35,19 +44,21 @@ context of an _instance_ of that class.
You can also declare nested nested groups using the `describe` or `context` You can also declare nested nested groups using the `describe` or `context`
methods: methods:


describe Order to ```ruby
context "with no items" do describe Order do
it "behaves one way" do context "with no items" do
# ... it "behaves one way" do
end # ...
end end

end
context "with one item" do
it "behaves another way" do context "with one item" do
# ... it "behaves another way" do
end # ...
end
end end
end
end
```


## aliases ## aliases


Expand All @@ -62,19 +73,21 @@ You can declare examples within a group using any of `it`, `specify`, or
Declare a shared example group using `shared_examples`, and then include it Declare a shared example group using `shared_examples`, and then include it
in any group using `include_examples`. in any group using `include_examples`.


shared_examples "collections" do |collection_class| ```ruby
it "is empty when first created" do shared_examples "collections" do |collection_class|
collection_class.new.should be_empty it "is empty when first created" do
end collection_class.new.should be_empty
end end
end


describe Array do describe Array do
include_examples "collections", Array include_examples "collections", Array
end end


describe Hash do describe Hash do
include_examples "collections", Hash include_examples "collections", Hash
end end
```


Nearly anything that can be declared within an example group can be declared Nearly anything that can be declared within an example group can be declared
within a shared example group. This includes `before`, `after`, and `around` within a shared example group. This includes `before`, `after`, and `around`
Expand All @@ -96,39 +109,45 @@ and filtering before and after hooks.
Although you probably won't ever need this unless you are writing an Although you probably won't ever need this unless you are writing an
extension, you can access it from an example like this: extension, you can access it from an example like this:


it "does something" do ```ruby
example.metadata[:description].should eq("does something") it "does something" do
end example.metadata[:description].should eq("does something")
end
```


### `described_class` ### `described_class`


When a class is passed to `describe`, you can access it from an example When a class is passed to `describe`, you can access it from an example
using the `described_class` method, which is a wrapper for using the `described_class` method, which is a wrapper for
`example.metadata[:described_class]`. `example.metadata[:described_class]`.


describe Widget do ```ruby
example do describe Widget do
described_class.should equal(Widget) example do
end described_class.should equal(Widget)
end end
end
```


This is useful in extensions or shared example groups in which the specific This is useful in extensions or shared example groups in which the specific
class is unknown. Taking the shared examples example from above, we can class is unknown. Taking the shared examples example from above, we can
clean it up a bit using `described_class`: clean it up a bit using `described_class`:


shared_examples "collections" do ```ruby
it "is empty when first created" do shared_examples "collections" do
described.new.should be_empty it "is empty when first created" do
end described.new.should be_empty
end end
end


describe Array do describe Array do
include_examples "collections" include_examples "collections"
end end


describe Hash do describe Hash do
include_examples "collections" include_examples "collections"
end end
```


## the `rspec` command ## the `rspec` command


Expand All @@ -146,52 +165,59 @@ Run `rspec --help` to see the complete list.
Start with a simple example of behavior you expect from your system. Do Start with a simple example of behavior you expect from your system. Do
this before you write any implementation code: this before you write any implementation code:


# in spec/calculator_spec.rb ```ruby
describe Calculator do # in spec/calculator_spec.rb
it "add(x,y) returns the sum of its arguments" do describe Calculator do
Calculator.new.add(1, 2).should eq(3) it "add(x,y) returns the sum of its arguments" do
end Calculator.new.add(1, 2).should eq(3)
end end
end
```


Run this with the rspec command, and watch it fail: Run this with the rspec command, and watch it fail:


$ rspec spec/calculator_spec.rb ```
./spec/calculator_spec.rb:1: uninitialized constant Calculator $ rspec spec/calculator_spec.rb
./spec/calculator_spec.rb:1: uninitialized constant Calculator
```


Implement the simplest solution: Implement the simplest solution:


# in lib/calculator.rb ```ruby
class Calculator # in lib/calculator.rb
def add(a,b) class Calculator
a + b def add(a,b)
end a + b
end end
end
```


Be sure to require the implementation file in the spec: Be sure to require the implementation file in the spec:


# in spec/calculator_spec.rb ```ruby
# - RSpec adds ./lib to the $LOAD_PATH # in spec/calculator_spec.rb
require "calculator" # - RSpec adds ./lib to the $LOAD_PATH
require "calculator"
```


Now run the spec again, and watch it pass: Now run the spec again, and watch it pass:


$ rspec spec/calculator_spec.rb ```
. $ rspec spec/calculator_spec.rb
.
Finished in 0.000315 seconds Finished in 0.000315 seconds
1 example, 0 failures 1 example, 0 failures
```


Use the `documentation` formatter to see the resulting spec: Use the `documentation` formatter to see the resulting spec:


$ rspec spec/calculator_spec.rb --format doc ```
Calculator add $ rspec spec/calculator_spec.rb --format doc
returns the sum of its arguments Calculator add

returns the sum of its arguments
Finished in 0.000379 seconds
1 example, 0 failures

## install
gem install rspec # for rspec-core, rspec-expectations, rspec-mocks Finished in 0.000379 seconds
gem install rspec-core # for rspec-core only 1 example, 0 failures
```


0 comments on commit d13d28f

Please sign in to comment.