こんにちは!とびろぐ管理人のとびうおです。
今回はLaravel10にて、簡単にテストを書く方法について詳しく解説していきたいと思います!
![とびうおくん](https://i0.wp.com/tobilog.net/wp-content/uploads/2021/01/C68848CD-D4C5-460B-B744-BEFA75315F89.jpg?resize=300%2C300&ssl=1)
今回はあるエンドポイントに対して、
リクエストを送った際に期待するページが
レスポンスされるかどうかという
テストを書いていくよ!
前提
まず今回テストするのはすごい簡単なページです。
http://localhost/hello にアクセスするとhelloというページが返ってくるというページです。
<web.php>
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-30.png?resize=1024%2C50&ssl=1)
<HelloController>
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-31.png?resize=914%2C730&ssl=1)
<hello.blade.php>
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-32.png?resize=1024%2C361&ssl=1)
![とびうおくん](https://i0.wp.com/tobilog.net/wp-content/uploads/2021/01/C68848CD-D4C5-460B-B744-BEFA75315F89.jpg?resize=300%2C300&ssl=1)
このくらい簡単な方が、
テストを書きやすいでしょ?
実際にテストを書いてみる
余談
では、ここで testを書いてみましょう。
./vendor/bin/sail php artisan test
と試しに打つと、
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-33.png?resize=1024%2C337&ssl=1)
![とびうおくん](https://i0.wp.com/tobilog.net/wp-content/uploads/2021/01/C68848CD-D4C5-460B-B744-BEFA75315F89.jpg?resize=300%2C300&ssl=1)
24 passedって!
この前の記事で入れたbreezeってテストまで書いてくれるのか…
![](https://i0.wp.com/tobilog.net/wp-content/uploads/2021/01/テクノロジー単発記事.png?resize=320%2C180&ssl=1)
まあ、それは置いておいて、実際にテストを書いてみましょう
本題
今回はhelloControllerのテストを書くので、以下のコマンドでテストファイルを作成します。
./vendor/bin/sail php artisan make:test HelloControllerTest
ローカルの方は↓でどうぞ
php artisan make:test HelloControllerTest
そうすることで、
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-34.png?resize=560%2C260&ssl=1)
無事、HelloControllerTestが作成されましたね。
そしたらここにテストを書いていきましょう。
HelloControllerの中身を見てみると親切なことに例が書いてありますね。
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-37.png?resize=1024%2C825&ssl=1)
そこで、16行目を以下のように書き換えてみます
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-36.png?resize=1024%2C826&ssl=1)
16行目を
$response = $this->get('/hello');
にしてみました。
再度ここで
./vendor/bin/sail php artisan test
を打つと、無事HelloControllerTestが成功していることが分かりますね。
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-38.png?resize=766%2C137&ssl=1)
![](https://i0.wp.com/www19.a8.net/0.gif?resize=1%2C1&ssl=1)
![](https://i0.wp.com/www16.a8.net/0.gif?resize=1%2C1&ssl=1)
![](https://i0.wp.com/www12.a8.net/0.gif?resize=1%2C1&ssl=1)
![](https://i0.wp.com/www19.a8.net/0.gif?resize=1%2C1&ssl=1)
より綺麗に書いてみる
これだけでも /hello というエンドポイントにアクセスした際に200が返ってくるというテストはかけましたが、もう少し詳しく、かつ便利に書いていきたいですね。
なので以下のように書き換えていきます。
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-40.png?resize=1024%2C933&ssl=1)
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class HelloControllerTest extends TestCase
{
private string $route_hello;
private string $view_hello;
public function setUp(): void
{
parent::setUp();
$this->route_hello = route('hello');
$this->view_hello = 'hello'; //ここはviewの名前を書く
}
public function test_response_hello(): void
{
$response = $this->get($this->route_hello);
$response->assertViewIs($this->view_hello);
}
}
このように書いて、再度実行してみると
![](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-41.png?resize=684%2C134&ssl=1)
ちゃんと成功していることがわかります。
なぜこのように書くのか
では、1つ目のコードと2つ目のコードを比較してみましょう。
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-36.png?resize=1024%2C826&ssl=1)
![Laravel10でのテストの書き方](https://i0.wp.com/tobilog.net/wp-content/uploads/2023/04/image-40.png?resize=1024%2C933&ssl=1)
右の方が長くて書くのが面倒くさそう…と思おうかもしれません。
しかし、右のコードは今後、urlが変わった際も対応できますし、しっかりviewが返ってきているのかというところまでテストできます。
![とびうおくん](https://i0.wp.com/tobilog.net/wp-content/uploads/2021/01/C68848CD-D4C5-460B-B744-BEFA75315F89.jpg?resize=300%2C300&ssl=1)
せっかくテストを書くなら、
丁寧に書こう!
まとめ
Laravel10ではかなり簡単にテストを書くことができます!
ぜひ、みなさんもエンドポイントとレスポンスくらいのテストは書いて、安心して開発をしてみてはいかがでしょうか?
![とびうおくん](https://i0.wp.com/tobilog.net/wp-content/uploads/2021/01/C68848CD-D4C5-460B-B744-BEFA75315F89.jpg?resize=300%2C300&ssl=1)
が、頑張ってテスト書くもん!
その他の記事
とびろぐではこれ以外にもLaravelの記事を多く書いています!
ぜひみてみてください!
![](https://i0.wp.com/tobilog.net/wp-content/uploads/2021/01/81868173-E931-44CF-9D78-6BC5E095F433-2-scaled.jpg?resize=160%2C90&ssl=1)
![](https://i0.wp.com/www19.a8.net/0.gif?resize=1%2C1&ssl=1)
![](https://i0.wp.com/www16.a8.net/0.gif?resize=1%2C1&ssl=1)
![](https://i0.wp.com/www12.a8.net/0.gif?resize=1%2C1&ssl=1)
![](https://i0.wp.com/www19.a8.net/0.gif?resize=1%2C1&ssl=1)
コメント