# RSpec

## Subject should match what is being described

Instead of:

{% tabs %}
{% tab title="Ruby" %}
{% code title="spec.rb" %}

```ruby
subject { instance }

before do
  instance.update!(something: something)
end

expect(subject).to ...
```

{% endcode %}
{% endtab %}
{% endtabs %}

It is clearer to handle the update in `subject` and then frame the assertion as a change.

```ruby
subject { instance.update!(something: something) }

expect { subject }.to change { ... }
```

## `.to match` vs. `.to eq`

Unlike in Jest, subset matchers only work with the looser `.to match`. When using Jest, `expect.objectContaining`, etc. can be used with both `.toEqual` and `.toBe`. However, `a_hash_including`, etc. will simply fail when used with `.to eq`! This is especially frustrating because nothing about the error message indicates that the failure comes from using the wrong matcher.

## Diffing JSON blobs

RSpec inlines JSON blobs by default, which makes diffs very hard to detect. The [`super_diff`](https://github.com/mcmire/super_diff) gem helps with this by printing the diff line-by-line.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timhwang21.gitbook.io/index/programming/ruby/rspec.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
