Skip to content

Instantly share code, notes, and snippets.

@yujinakayama
Last active July 26, 2017 05:17
Show Gist options
  • Select an option

  • Save yujinakayama/5ec82ca591906b6058be to your computer and use it in GitHub Desktop.

Select an option

Save yujinakayama/5ec82ca591906b6058be to your computer and use it in GitHub Desktop.
Example of switching positive/negative in compound expectations
describe 'some destructive method' do
it 'destroys only relevant things' do
expect { do_something_destructive }.to change { things_to_destroy.reload.count }.to(0)
.and.not_to change { things_not_to_destroy.reload.count }
# ^^^^^^^ This is what I want to do and just a fictional syntax.
end
end
@myronmarston
Copy link

Here's what you can do that works:

RSpec::Matchers.define_negated_matcher :avoid_changing, :change

describe 'some destructive method' do
  it 'destroys only relevant things' do
    expect {
      do_something_destructive
    }.to change { things_to_destroy.reload.count }.to(0)
     .and avoid_changing { things_not_to_destroy.reload.count }
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment