Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active November 17, 2015 07:56
Show Gist options
  • Save YumaInaura/fde74a72d7fa3c670a71 to your computer and use it in GitHub Desktop.
Save YumaInaura/fde74a72d7fa3c670a71 to your computer and use it in GitHub Desktop.
RSpec - change の基本動作。 ( by / from / to の使い方 ) ref: http://qiita.com/Yinaura/items/62dc0d87f968f43e335b
x = 1
expect { x += 1 }.to change { x }
# => true
x = 1
expect { x += 2 }.to change { x }
# => true
x = 1
expect { x += 1; x -= 1; }.to change { x }
# => RSpec::Expectations::ExpectationNotMetError: result should have changed, but is still 1
x = 1
expect { x += 5 }.to change { x }.by(5)
# => true
x = 1
expect { x -= 5 }.to change { x }.by(-5)
# => true
x = 3
expect { x += 4 }.to change { x }.by(7)
# => RSpec::Expectations::ExpectationNotMetError: result should have been changed by 7, but was changed by 4
x = 3
expect { x += 4 }.to change { x }.by(4)
# => true
x = 3
expect { x += 4 }.to change { x }.from(3).to(7)
# => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment