🖥

Basic behavior of RSpec-change. (How to use by / from / to)

2019/04/16に公開

change

It just verifies that the state has changed. I don't care about "what changed to what" or "how changed".

If you add 1 to x, change holds.

 x = 1 expect { x += 1 }.to change { x } # => true 

Even if 2 is added to x, change holds.

 x = 1 expect { x += 2 }.to change { x } # => true 

Add 1 to x and subtract 1 more. Since the state has not changed, change does not hold.

 x = 1 expect { x += 1; x -= 1; }.to change { x } # => RSpec::Expectations::ExpectationNotMetError: result should have changed, but is still 1 

change.by

Verify the increase or decrease of the value with the relative value.

If the value is increased by 5, change.by(5) holds.

 x = 1 expect { x += 5 }.to change { x }.by(5) # => true 

If the value decreases by 5, change.by(-5) holds.

 x = 1 expect { x -= 5 }.to change { x }.by(-5) # => true 

As we do not verify "absolute value" after increase and decrease, we are careful. For example, when calculating 3 + 4, change.by(7) does not hold.

 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 

The increased value is 4, so it is change.by(4) that holds.

 x = 3 expect { x += 4 }.to change { x }.by(4) # => true 

change.from.to

Verify what has changed from what state. For example, if you calculate x = 3 + 4 , the value changes from 3 to 7.

Therefore, change.from(3).to(7) holds.

 x = 3 expect { x += 4 }.to change { x }.from(3).to(7) # => true 

Original by

RSpec - change の基本動作。 ( by / from / to の使い方 )

About

About this translattion

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-16

Discussion