Created
July 12, 2017 07:12
-
-
Save zaru/2c102235b952541c57854778e8cc6beb to your computer and use it in GitHub Desktop.
他のクラスに依存しているクラスのテスト・モックスタブ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class A | |
| def perform | |
| b = B.new | |
| b.execute | |
| end | |
| end | |
| class B | |
| def execute | |
| "本物" | |
| end | |
| end | |
| it "" do | |
| mock = B.new | |
| allow(B).to receive(:new).and_return(mock) | |
| allow(mock).to receive(:execute).and_return("stub method") | |
| A.new.perform | |
| expect(mock).to have_received(:execute).once | |
| end |
Author
ありがとうございます! 参考になります。
今回の案件だと、Aクラスに手を入れにくい状況だったので、外からモックを無理やり作る方向で模索していました。指摘したもらった通りAクラス自体を改善していくのが良いと思います。
また、 allow_any_instance_of を使おうかと思ったんですが、この記事を読んで止めました。
今回の案件だと、Aクラスに手を入れにくい状況だったので、外からモックを無理やり作る方向で模索していました。指摘したもらった通りAクラス自体を改善していくのが良いと思います。
なるほど、それなら仕方ないですね。
また、 allow_any_instance_of を使おうかと思ったんですが、この記事を読んで止めました。
はい、allow_any_instance_ofはできるだけ使わない方がいいですね。(といいつつ、僕は今もたまに使いますがw)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
あわせて読みたい
注意点1:モックが使いやすい設計にしましょう