前提・実現したいこと
cakephpでのユニットテストを初めて書く者です
facebookログインをしているユーザーのみが実行可能なメソッドのテストをしたいと思い、まずログインしていない状態・ログインしている状態を作り出したテストメソッドを投げ、うまくいっているか確認したいと思いました
発生している問題・エラーメッセージ
サイトへfacebookログインしていないとリダイレクトされる部分の処理なので、ユーザーがログインしている場合とそうでない場合をまずテストしたいのですが両方共同じ結果になってしまいます
一つ目のユーザーログインしていない前提のものは、リダイレクトされたという趣旨のレスポンス
二つ目の方は、200が返ってこないといけないと思っているのですがこの前提そのものが違います出ようか?
該当のテストコード
<?php namespace App\Test\TestCase\Controller; use App\Controller\PaymentsController; use Cake\TestSuite\IntegrationTestCase; class PaymentsControllerTest extends IntegrationTestCase { /** * Fixtures * * @var array */ // public $fixtures = [ // 'app.users', // 'app.clubs', // 'app.club_plans', // 'app.subscriptions', // 'app.user_infos', // 'app.clubs_users' // ]; public function testPayNoUser() { $clubId = 'テスト用DBに入れたクラブID'; $this->get('/payments/pay/'. $clubId); $this->assertResponseOk(); // セッションはない $this->assertSession(null, 'Auth.User.id'); $this->assertRedirectContains("/authentications/login_for_pay/" . $clubId); } public function testPay() { // セッションデータのセット $this->session([ 'Auth' => [ 'Users' => [ 'id' => 'テスト用のDBに入れたユーザーID', ] ] ]); $clubId = 'テスト用DBに入れたクラブID'; $this->get('/payments/pay/'. $clubId); $this->assertResponseOk(); $this->assertSession(1, 'Auth.User.id'); } }
レスポンス
PHPUnit 6.5.14 by Sebastian Bergmann and contributors. FF 2 / 2 (100%) Time: 356 ms, Memory: 10.00MB There were 2 failures: 1) App\Test\TestCase\Controller\PaymentsControllerTest::testPayNoUser Status code is not between 200 and 204 Failed asserting that 302 is equal to 204 or is less than 204. /Applications/MAMP/htdocs/app_name/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:794 /Applications/MAMP/htdocs/app_name/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:711 /Applications/MAMP/htdocs/app_name/tests/TestCase/Controller/PaymentsControllerTest.php:56 2) App\Test\TestCase\Controller\PaymentsControllerTest::testPay Status code is not between 200 and 204 Failed asserting that 302 is equal to 204 or is less than 204. /Applications/MAMP/htdocs/app_name/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:794 /Applications/MAMP/htdocs/app_name/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:711 /Applications/MAMP/htdocs/app_name/tests/TestCase/Controller/PaymentsControllerTest.php:76 FAILURES! Tests: 2, Assertions: 8, Failures: 2.
元のコントローラー
public function pay($clubId = null) { if(!$this->Auth->user()) { return $this->redirect("/authentications/login_for_pay/" . $clubId); } // Check club id if (empty($clubId)) { $this->Flash->error(__('Invalid request.')); return $this->redirect($this->referer()); } // Check club data $club = $this->clubs->find() ->contain(['clubPlans']) ->where(['clubs.id' => $clubId]) ->first(); if (empty($club)) { $this->Flash->error(__('There is no club data.')); return $this->redirect($this->referer()); } ・・・
下記の記事などを参考にしています
・公式 https://book.cakephp.org/3/ja/development/testing.html#integration-testing
・コントローラー周りのテストについて
https://qiita.com/o0h/items/ea8d1b3604c37c3a8287
・各種アクションに関するテスト
https://pachicourse.com/archives/486
・テストの自動化について
https://insight.hiliberate.biz/?p=2313
ただ、そもそもどのようなレスポンスが返って来ればいいのかなど正直まだ把握しきれていません
テスト結果の読み方が間違っている・何か勘違いしていそうなどありましたらご指摘いただければと思います
補足情報(FW/ツールのバージョンなど)
cakephp3.5
mysql
PHP 7.2.22
サーバー MAMP
回答1件
あなたの回答
tips
プレビュー