description |
---|
Certain RSpec patterns. |
Instead of:
{% tabs %} {% tab title="Ruby" %} {% code title="spec.rb" %}
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.
subject { instance.update!(something: something) }
expect { subject }.to change { ... }
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.
RSpec inlines JSON blobs by default, which makes diffs very hard to detect. The super_diff
gem helps with this by printing the diff line-by-line.