changeマッチャーとは

#### changeマッチャー　変化しているかを調べる
>### change matcher

コードのブロックが変更可能な状態を変更することを指定するために`change`マッチャーが使われる。二つのどれかを使って何が変更されるか指定することができる。
>The change matcher is used to specify that a block of code changes some mutable state. You can specify what will change using either of two forms:

>* `expect { do_something }.to change(object, :attribute)`
>* `expect { do_something }.to change { object.attribute }`

* `change(object, :attribute)`や`change { object.attribute }`が状態を変えるブロックだと考える

`from`,`to`または `one of by`, `by_at_most`, `by_at_least`からチェインすることによって`change`をより修飾することができる
>You can further qualify the change by chaining from and/or to or one of by, by_at_most, by_at_least.

* `.from(0).to(1)、.by(2)`の部分でチェインされているのでここで詳細に指定できると考える
* 以下のようなものを使うことがつかることができる

>### Background
```rb
class Counter
  class << self
    def increment
      @count ||= 0
      @count += 1 # 一以上にならない
    end

    def count
      @count ||= 0 # 0のまま
    end
  end
end
```
>### Expect change

```rb
require "counter"

RSpec.describe Counter, "#increment" do
  it "should increment the count" do
    expect { Counter.increment }.to change { Counter.count }.from(0).to(1)
  end

  # deliberate failure
  it "should increment the count by 2" do
    expect { Counter.increment }.to change { Counter.count }.by(2) # 変化量は２か?
  end
end
```
```
Run options: include {:full_description=>/Counter\#increment/}

expected `Counter.count` to have changed by 2, but was changed by 1

  0) Counter#increment should increment the count by 2
     Failure/Error: expect { Counter.increment }.to change { Counter.count }.by(2)
       expected `Counter.count` to have changed by 2, but was changed by 1
     # ./spec/models/user_spec.rb:66:in `block (2 levels) in <top (required)>'
2 examples, 1 failure, 1 passed
```
>### Expect no change
```rb
require "counter"

RSpec.describe Counter, "#increment" do
  it "should not increment the count by 1 (using not_to)" do
    expect { Counter.increment }.not_to change { Counter.count } # Counter.countに変化していないか？
  end

  it "should not increment the count by 1 (using to_not)" do
    expect { Counter.increment }.to_not change { Counter.count }
  end
end
```
```
Run options: include {:full_description=>/Counter\#increment/}

expected `Counter.count` not to have changed, but did change from 0 to 1

  0) Counter#increment should not increment the count by 1 (using not_to)
     Failure/Error: expect { Counter.increment }.not_to change { Counter.count }
       expected `Counter.count` not to have changed, but did change from 0 to 1
     # ./spec/models/user_spec.rb:61:in `block (2 levels) in <top (required)>'

expected `Counter.count` not to have changed, but did change from 1 to 2

  0) Counter#increment should not increment the count by 1 (using to_not)
     Failure/Error: expect { Counter.increment }.to_not change { Counter.count }
       expected `Counter.count` not to have changed, but did change from 1 to 2
     # ./spec/models/user_spec.rb:65:in `block (2 levels) in <top (required)>'
2 examples, 2 failures, 0 passed
```

https://rspec.info/features/3-13/rspec-expectations/built-in-matchers/change/

>Change observation
```rb
expect { object.action }.to change(object, :value).from(old).to(new)
expect { object.action }.to change(object, :value).by(delta)
expect { object.action }.to change(object, :value).by_at_least(minimum_delta)
expect { object.action }.to change(object, :value).by_at_most(maximum_delta)
```
>Examples
```
expect { a += 1 }.to change { a }.by(1)
expect { a += 3 }.to change { a }.from(2)
expect { a += 3 }.to change { a }.by_at_least(2)
```
https://rspec.info/features/3-13/rspec-expectations/built-in-matchers/#:~:text=Change%20observation,by_at_least(2)

### デルタとは？
>デルタという語は、一般的に、物理的もしくは仮想的なオブジェクトの速度、位置、加速度の変化を表す時に使われます。また、音波の振幅または周波数の変化を表す時にも使われます。

>例えば、画面上でオブジェクトが左から右へ移動した距離を述べる時には、delta x または Δx という表記が使用できます。

>同様に、新しい値 X とその古い値が与えられた時、次のようにデルタを計算できます。

```js
let deltaX = newX - oldX;
```
https://developer.mozilla.org/ja/docs/Glossary/Delta#:~:text=%E3%83%87%E3%83%AB%E3%82%BF%E3%81%A8%E3%81%84%E3%81%86%E8%AA%9E,%2D%20oldX%3B