0
例:Laravelでエンドポイントを呼び出す際に、コントローラに注入されたオブジェクトをモックする方法は?
私はエンドポイント/something/cool
とルートファイルがあります。
$router->get('/something/cool', [
'uses' => '[email protected]',
]);
を私はMyController
という名前のコントローラを持っています。 MyController
には、myFunctionOne
という名前の関数があります。 myFunctionOne
パラメータには、私は注入サービスクラスMyService
があります。 MyService
には、外部API callExternalApi()
を呼び出す関数があります。私は、機能テストを持っている反対側に
class MyController
{
public function myFunctionOne(MyService $myService)
{
$myService->callExternalApi();
// do some other things..
}
}
:
はここに私のコントローラがどのように見えるかだ
class SomethingCoolTest extends TestCase
{
public function testSomethingCool()
{
// callin my Route Endpoint (real http call to my app)
$this->get('/something/cool', [])->response;
// do some assertions..
}
}
私の質問は:どのように私は、コントローラを注入サービスを模擬することができ、それは外部サービスを呼び出しているからですか?私は予想以上に簡単だった