0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LaravelでMockeryを使う際の注意点

Posted at

めちゃくちゃ便利なMockery。テストを書く方なら多分結構使ったことがあるかなと思います。
今回はMockeryを使う際の注意点を紹介します。

Mockeryはprotectedなメソッドには使えない

意外な落とし穴としてはこれが上げられるかなと思います。

Mockeryprotectedなメソッドには使えないのです。
ただ、どうしてもprotectedでメソッド宣言したい、でもテストするときに必要だしな ~ みたいなパターンあると思います。

めちゃくちゃ親切なことにそれ専用のメソッドが用意されています。

shouldAllowMockingProtectedMethodsを使おう

protected専用のメソッドです。
こいつを使ってあげれば一発で解消します。(公式)

$service = \Mockery::mock('Class')->shouldAllowMockingProtectedMethods();

上記の様に呼び出してあげればokです :v_tone1:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?