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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
# rspec-core [](https://github.com/rspec/rspec-core/actions) [](https://codeclimate.com/github/rspec/rspec-core)
rspec-core provides the structure for writing executable examples of how your
code should behave, and an `rspec` command with tools to constrain which
examples get run and tailor the output.
## Install
gem install rspec # for rspec-core, rspec-expectations, rspec-mocks
gem install rspec-core # for rspec-core only
rspec --help
Want to run against the `main` branch? You'll need to include the dependent
RSpec repos as well. Add the following to your `Gemfile`:
```ruby
%w[rspec rspec-core rspec-expectations rspec-mocks rspec-support].each do |lib|
gem lib, :git => "https://github.com/rspec/#{lib}.git", :branch => 'main'
end
```
## Basic Structure
RSpec uses the words "describe" and "it" so we can express concepts like a conversation:
"Describe an order."
"It sums the prices of its line items."
```ruby
RSpec.describe Order do
it "sums the prices of its line items" do
order = Order.new
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),
:quantity => 2
)))
expect(order.total).to eq(Money.new(5.55, :USD))
end
end
```
The `describe` method creates an [ExampleGroup](http://rubydoc.info/gems/rspec-core/RSpec/Core/ExampleGroup). Within the
block passed to `describe` you can declare examples using the `it` method.
Under the hood, an example group is a class in which the block passed to
`describe` is evaluated. The blocks passed to `it` are evaluated in the
context of an _instance_ of that class.
## Nested Groups
You can also declare nested groups using the `describe` or `context`
methods:
```ruby
RSpec.describe Order do
context "with no items" do
it "behaves one way" do
# ...
end
end
context "with one item" do
it "behaves another way" do
# ...
end
end
end
```
Nested groups are subclasses of the outer example group class, providing
the inheritance semantics you'd want for free.
## Aliases
You can declare example groups using either `describe` or `context`.
For a top level example group, `describe` and `context` are available
off of `RSpec`. For backwards compatibility, they are also available
off of the `main` object and `Module` unless you disable monkey
patching.
You can declare examples within a group using any of `it`, `specify`, or
`example`.
## Shared Examples and Contexts
Declare a shared example group using `shared_examples`, and then include it
in any group using `include_examples`.
```ruby
RSpec.shared_examples "collections" do |collection_class|
it "is empty when first created" do
expect(collection_class.new).to be_empty
end
end
RSpec.describe Array do
include_examples "collections", Array
end
RSpec.describe Hash do
include_examples "collections", Hash
end
```
Nearly anything that can be declared within an example group can be declared
within a shared example group. This includes `before`, `after`, and `around`
hooks, `let` declarations, and nested groups/contexts.
You can also use the names `shared_context` and `include_context`. These are
pretty much the same as `shared_examples` and `include_examples`, providing
more accurate naming when you share hooks, `let` declarations, helper methods,
etc, but no examples.
## Metadata
rspec-core stores a metadata hash with every example and group, which
contains their descriptions, the locations at which they were
declared, etc, etc. This hash powers many of rspec-core's features,
including output formatters (which access descriptions and locations),
and filtering before and after hooks.
Although you probably won't ever need this unless you are writing an
extension, you can access it from an example like this:
```ruby
it "does something" do |example|
expect(example.metadata[:description]).to eq("does something")
end
```
### `described_class`
When a class is passed to `describe`, you can access it from an example
using the `described_class` method, which is a wrapper for
`example.metadata[:described_class]`.
```ruby
RSpec.describe Widget do
example do
expect(described_class).to equal(Widget)
end
end
```
This is useful in extensions or shared example groups in which the specific
class is unknown. Taking the collections shared example group from above, we can
clean it up a bit using `described_class`:
```ruby
RSpec.shared_examples "collections" do
it "is empty when first created" do
expect(described_class.new).to be_empty
end
end
RSpec.describe Array do
include_examples "collections"
end
RSpec.describe Hash do
include_examples "collections"
end
```
## A Word on Scope
RSpec has two scopes:
* **Example Group**: Example groups are defined by a `describe` or
`context` block, which is eagerly evaluated when the spec file is
loaded. The block is evaluated in the context of a subclass of
`RSpec::Core::ExampleGroup`, or a subclass of the parent example group
when you're nesting them.
* **Example**: Examples -- typically defined by an `it` block -- and any other
blocks with per-example semantics -- such as a `before(:example)` hook -- are
evaluated in the context of
an _instance_ of the example group class to which the example belongs.
Examples are _not_ executed when the spec file is loaded; instead,
RSpec waits to run any examples until all spec files have been loaded,
at which point it can apply filtering, randomization, etc.
To make this more concrete, consider this code snippet:
``` ruby
RSpec.describe "Using an array as a stack" do
def build_stack
[]
end
before(:example) do
@stack = build_stack
end
it 'is initially empty' do
expect(@stack).to be_empty
end
context "after an item has been pushed" do
before(:example) do
@stack.push :item
end
it 'allows the pushed item to be popped' do
expect(@stack.pop).to eq(:item)
end
end
end
```
Under the covers, this is (roughly) equivalent to:
``` ruby
class UsingAnArrayAsAStack < RSpec::Core::ExampleGroup
def build_stack
[]
end
def before_example_1
@stack = build_stack
end
def it_is_initially_empty
expect(@stack).to be_empty
end
class AfterAnItemHasBeenPushed < self
def before_example_2
@stack.push :item
end
def it_allows_the_pushed_item_to_be_popped
expect(@stack.pop).to eq(:item)
end
end
end
```
To run these examples, RSpec would (roughly) do the following:
``` ruby
example_1 = UsingAnArrayAsAStack.new
example_1.before_example_1
example_1.it_is_initially_empty
example_2 = UsingAnArrayAsAStack::AfterAnItemHasBeenPushed.new
example_2.before_example_1
example_2.before_example_2
example_2.it_allows_the_pushed_item_to_be_popped
```
## The `rspec` Command
When you install the rspec-core gem, it installs the `rspec` executable,
which you'll use to run rspec. The `rspec` command comes with many useful
options.
Run `rspec --help` to see the complete list.
## Store Command Line Options `.rspec`
You can store command line options in a `.rspec` file in the project's root
directory, and the `rspec` command will read them as though you typed them on
the command line.
## Get Started
Start with a simple example of behavior you expect from your system. Do
this before you write any implementation code:
```ruby
# in spec/calculator_spec.rb
RSpec.describe Calculator do
describe '#add' do
it 'returns the sum of its arguments' do
expect(Calculator.new.add(1, 2)).to eq(3)
end
end
end
```
Run this with the rspec command, and watch it fail:
```
$ rspec spec/calculator_spec.rb
./spec/calculator_spec.rb:1: uninitialized constant Calculator
```
Address the failure by defining a skeleton of the `Calculator` class:
```ruby
# in lib/calculator.rb
class Calculator
def add(a, b)
end
end
```
Be sure to require the implementation file in the spec:
```ruby
# in spec/calculator_spec.rb
# - RSpec adds ./lib to the $LOAD_PATH
require "calculator"
```
Now run the spec again, and watch the expectation fail:
```
$ rspec spec/calculator_spec.rb
F
Failures:
1) Calculator#add returns the sum of its arguments
Failure/Error: expect(Calculator.new.add(1, 2)).to eq(3)
expected: 3
got: nil
(compared using ==)
# ./spec/calcalator_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.00131 seconds (files took 0.10968 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/calcalator_spec.rb:5 # Calculator#add returns the sum of its arguments
```
Implement the simplest solution, by changing the definition of `Calculator#add` to:
```ruby
def add(a, b)
a + b
end
```
Now run the spec again, and watch it pass:
```
$ rspec spec/calculator_spec.rb
.
Finished in 0.000315 seconds
1 example, 0 failures
```
Use the `documentation` formatter to see the resulting spec:
```
$ rspec spec/calculator_spec.rb --format doc
Calculator
#add
returns the sum of its arguments
Finished in 0.000379 seconds
1 example, 0 failures
```
## Contributing
Once you've set up the environment, you'll need to cd into the working
directory of whichever repo you want to work in. From there you can run the
specs and cucumber features, and make patches.
NOTE: You do not need to use rspec-dev to work on a specific RSpec repo. You
can treat each RSpec repo as an independent project.
* [Build details](BUILD_DETAIL.md)
* [Code of Conduct](CODE_OF_CONDUCT.md)
* [Detailed contributing guide](CONTRIBUTING.md)
* [Development setup guide](DEVELOPMENT.md)
## Also see
* [https://github.com/rspec/rspec](https://github.com/rspec/rspec)
* [https://github.com/rspec/rspec-expectations](https://github.com/rspec/rspec-expectations)
* [https://github.com/rspec/rspec-mocks](https://github.com/rspec/rspec-mocks)
* [https://github.com/rspec/rspec-rails](https://github.com/rspec/rspec-rails)
|