Skip to content

Commit

Permalink
Merge pull request #1447 from threeplanetssoftware/update-documentati…
Browse files Browse the repository at this point in the history
…on-code-blocks

Updated Ruby code block markdown syntax
  • Loading branch information
pirj committed Jan 29, 2024
2 parents d0bb212 + fa8514a commit 96a3e5a
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 72 deletions.
10 changes: 10 additions & 0 deletions features/README.md
Expand Up @@ -2,40 +2,50 @@

rspec-expectations is used to define expected outcomes.

```ruby
RSpec.describe Account do
it "has a balance of zero when first created" do
expect(Account.new.balance).to eq(Money.new(0))
end
end
```

## Basic structure

The basic structure of an rspec expectation is:

```ruby
expect(actual).to matcher(expected)
expect(actual).not_to matcher(expected)
```

Note: You can also use `expect(..).to_not` instead of `expect(..).not_to`.
One is an alias to the other, so you can use whichever reads better to you.

#### Examples

```ruby
expect(5).to eq(5)
expect(5).not_to eq(4)
```

## What is a matcher?

A matcher is any object that responds to the following methods:

```ruby
matches?(actual)
failure_message
```

These methods are also part of the matcher protocol, but are optional:

```ruby
does_not_match?(actual)
failure_message_when_negated
description
supports_block_expectations?
```

RSpec ships with a number of built-in matchers and a DSL for writing custom
matchers.
Expand Down
40 changes: 40 additions & 0 deletions features/built_in_matchers/README.md
Expand Up @@ -7,27 +7,36 @@ respectively on an object. Most matchers can also be accessed using the `(...).s

e.g.

```ruby
expect(result).to eq(3)
expect(list).not_to be_empty
pi.should be > 3
```

## Object identity

```ruby
expect(actual).to be(expected) # passes if actual.equal?(expected)
```

## Object equivalence

```ruby
expect(actual).to eq(expected) # passes if actual == expected
```

## Optional APIs for identity/equivalence

```ruby
expect(actual).to eql(expected) # passes if actual.eql?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)

# NOTE: `expect` does not support `==` matcher.
```

## Comparisons

```ruby
expect(actual).to be > expected
expect(actual).to be >= expected
expect(actual).to be <= expected
Expand All @@ -40,100 +49,131 @@ e.g.
expect(actual).to end_with expected

# NOTE: `expect` does not support `=~` matcher.
```

## Types/classes/response

```ruby
expect(actual).to be_instance_of(expected)
expect(actual).to be_kind_of(expected)
expect(actual).to respond_to(expected)
```

## Truthiness and existentialism

```ruby
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true # passes if actual == true
expect(actual).to be_falsey # passes if actual is falsy (nil or false)
expect(actual).to be false # passes if actual == false
expect(actual).to be_nil # passes if actual is nil
expect(actual).to exist # passes if actual.exist? and/or actual.exists? are truthy
expect(actual).to exist(*args) # passes if actual.exist?(*args) and/or actual.exists?(*args) are truthy
```

## Expecting errors

```ruby
expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")
```

## Expecting throws

```ruby
expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')
```

## Predicate matchers

```ruby
expect(actual).to be_xxx # passes if actual.xxx?
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)
```

### Examples

```ruby
expect([]).to be_empty
expect(:a => 1).to have_key(:a)
```

## Collection membership

```ruby
expect(actual).to include(expected)
expect(array).to match_array(expected_array)
# ...which is the same as:
expect(array).to contain_exactly(individual, elements)
```

### Examples

```ruby
expect([1, 2, 3]).to include(1)
expect([1, 2, 3]).to include(1, 2)
expect(:a => 'b').to include(:a => 'b')
expect("this string").to include("is str")
expect([1, 2, 3]).to contain_exactly(2, 1, 3)
expect([1, 2, 3]).to match_array([3, 2, 1])
```

## Ranges (1.9+ only)

```ruby
expect(1..10).to cover(3)
```

## Change observation

```ruby
expect { object.action }.to change(object, :value).from(old).to(new)
expect { object.action }.to change(object, :value).by(delta)
expect { object.action }.to change(object, :value).by_at_least(minimum_delta)
expect { object.action }.to change(object, :value).by_at_most(maximum_delta)
```

### Examples

```ruby
expect { a += 1 }.to change { a }.by(1)
expect { a += 3 }.to change { a }.from(2)
expect { a += 3 }.to change { a }.by_at_least(2)
```

## Satisfy

```ruby
expect(actual).to satisfy { |value| value == expected }
```

## Output capture

```ruby
expect { actual }.to output("some output").to_stdout
expect { actual }.to output("some error").to_stderr
```

## Block expectation

```ruby
expect { |b| object.action(&b) }.to yield_control
expect { |b| object.action(&b) }.to yield_with_no_args # only matches no args
expect { |b| object.action(&b) }.to yield_with_args # matches any args
expect { |b| object.action(&b) }.to yield_successive_args(*args) # matches args against multiple yields
```

### Examples

```ruby
expect { |b| User.transaction(&b) }.to yield_control
expect { |b| User.transaction(&b) }.to yield_with_no_args
expect { |b| 5.tap(&b) }.not_to yield_with_no_args # because it yields with `5`
expect { |b| 5.tap(&b) }.to yield_with_args(5) # because 5 == 5
expect { |b| 5.tap(&b) }.to yield_with_args(Integer) # because Integer === 5
expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
```
8 changes: 4 additions & 4 deletions features/built_in_matchers/all.feature
Expand Up @@ -2,19 +2,19 @@ Feature: `all` matcher

Use the `all` matcher to specify that a collection's objects all pass an expected matcher. This works on any enumerable object.

```ruby
```ruby
expect([1, 3, 5]).to all( be_odd )
expect([1, 3, 5]).to all( be_an(Integer) )
expect([1, 3, 5]).to all( be < 10 )
expect([1, 3, 4]).to all( be_odd ) # fails
```
```
The matcher also supports compound matchers:
```ruby
```ruby
expect([1, 3, 5]).to all( be_odd.and be < 10 )
expect([1, 4, 21]).to all( be_odd.or be < 10 )
```
```
If you are looking for "any" member of a collection that passes an expectation, look at the `include`-matcher.
Expand Down
4 changes: 2 additions & 2 deletions features/built_in_matchers/be.feature
Expand Up @@ -2,12 +2,12 @@ Feature: `be` matchers

There are several related "be" matchers:

```ruby
```ruby
expect(obj).to be_truthy # passes if obj is truthy (not nil or false)
expect(obj).to be_falsey # passes if obj is falsy (nil or false)
expect(obj).to be_nil # passes if obj is nil
expect(obj).to be # passes if obj is truthy (not nil or false)
```
```

Scenario: The `be_truthy` matcher
Given a file named "be_truthy_spec.rb" with:
Expand Down
4 changes: 2 additions & 2 deletions features/built_in_matchers/be_within.feature
Expand Up @@ -13,9 +13,9 @@ Feature: `be_within` matcher
Instead, you should use the `be_within` matcher to check that the value is within a delta of
your expected value:

```ruby
```ruby
expect(area_of_circle).to be_within(0.1).of(28.3)
```
```

Note that the difference between the actual and expected values must be smaller than your
delta; if it is equal, the matcher will fail.
Expand Down
4 changes: 2 additions & 2 deletions features/built_in_matchers/comparisons.feature
Expand Up @@ -3,12 +3,12 @@ Feature: Comparison matchers
RSpec provides a number of matchers that are based on Ruby's built-in operators. These
can be used for generalized comparison of values. E.g.

```ruby
```ruby
expect(9).to be > 6
expect(3).to be <= 3
expect(1).to be < 6
expect('a').to be < 'b'
```
```
Scenario: Numeric operator matchers
Given a file named "numeric_operator_matchers_spec.rb" with:
Expand Down
8 changes: 4 additions & 4 deletions features/built_in_matchers/contain_exactly.feature
Expand Up @@ -4,19 +4,19 @@ Feature: `contain_exactly` matcher
that disregards differences in the ordering between the actual and expected array.
For example:

```ruby
```ruby
expect([1, 2, 3]).to contain_exactly(2, 3, 1) # pass
expect([:a, :c, :b]).to contain_exactly(:a, :c ) # fail
```
```

This matcher is also available as `match_array`, which expects the expected array to be
given as a single array argument rather than as individual splatted elements. The above
could also be written as:

```ruby
```ruby
expect([1, 2, 3]).to match_array [2, 3, 1] # pass
expect([:a, :c, :b]).to match_array [:a, :c] # fail
```
```

Scenario: Array is expected to contain every value
Given a file named "contain_exactly_matcher_spec.rb" with:
Expand Down
4 changes: 2 additions & 2 deletions features/built_in_matchers/cover.feature
Expand Up @@ -5,11 +5,11 @@ Feature: `cover` matcher
expected objects. This works on any object that responds to `#cover?`
(such as a `Range`):

```ruby
```ruby
expect(1..10).to cover(5)
expect(1..10).to cover(4, 6)
expect(1..10).not_to cover(11)
```
```

Scenario: Range usage
Given a file named "range_cover_matcher_spec.rb" with:
Expand Down
4 changes: 2 additions & 2 deletions features/built_in_matchers/end_with.feature
Expand Up @@ -3,11 +3,11 @@ Feature: `end_with` matcher
Use the `end_with` matcher to specify that a string or array ends with the expected
characters or elements.

```ruby
```ruby
expect("this string").to end_with "string"
expect("this string").not_to end_with "stringy"
expect([0, 1, 2]).to end_with 1, 2
```
```

Scenario: String usage
Given a file named "example_spec.rb" with:
Expand Down
8 changes: 4 additions & 4 deletions features/built_in_matchers/equality.feature
Expand Up @@ -11,18 +11,18 @@ Feature: Equality matchers

rspec-expectations ships with matchers that align with each of these methods:

```ruby
```ruby
expect(a).to equal(b) # passes if a.equal?(b)
expect(a).to eql(b) # passes if a.eql?(b)
expect(a).to be == b # passes if a == b
```
```

It also ships with two matchers that have more of a DSL feel to them:

```ruby
```ruby
expect(a).to be(b) # passes if a.equal?(b)
expect(a).to eq(b) # passes if a == b
```
```

Scenario: Compare using eq (==)
Given a file named "compare_using_eq.rb" with:
Expand Down
4 changes: 2 additions & 2 deletions features/built_in_matchers/exist.feature
Expand Up @@ -2,9 +2,9 @@ Feature: `exist` matcher

The `exist` matcher is used to specify that something exists (as indicated by `#exist?` or `#exists?`):

```ruby
```ruby
expect(obj).to exist # passes if obj.exist? or obj.exists?
```
```

Scenario: Basic usage
Given a file named "exist_matcher_spec.rb" with:
Expand Down
8 changes: 4 additions & 4 deletions features/built_in_matchers/have_attributes.feature
Expand Up @@ -2,19 +2,19 @@ Feature: `have_attributes` matcher

Use the have_attributes matcher to specify that an object's attributes match the expected attributes:

```ruby
```ruby
Person = Struct.new(:name, :age)
person = Person.new("Jim", 32)

expect(person).to have_attributes(:name => "Jim", :age => 32)
expect(person).to have_attributes(:name => a_string_starting_with("J"), :age => (a_value > 30) )
```
```

The matcher will fail if actual doesn't respond to any of the expected attributes:

```ruby
```ruby
expect(person).to have_attributes(:name => "Jim", :color => 'red')
```
```

Scenario: Basic usage
Given a file named "basic_have_attributes_matcher_spec.rb" with:
Expand Down

0 comments on commit 96a3e5a

Please sign in to comment.