You are missing this on the second test
/** @test */
But just to understand. You want one test to be dependent on the other?
I would like to check if our registration has been activated and if not display this in the test. In the case it is not active the form is not shown and a test will be shown. So for the test I had
<?php
namespace Tests\Feature\Http\Controllers\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class RegisterControllerTest extends TestCase
{
/**
* Check for active registration
*
* @return void
*/
/** @test */
public function registration_not_active()
{
$response = $this->get(route('register'));
$response->assertStatus(500);
$this->assertTrue(true);
}
public function registration_active()
{
$response = $this->get(route('register'));
$response->assertStatus(200);
$response->assertViewIs('auth.register');
}
}
but it only runs the display test showing the registration has not been activated even if that is not true. How come?
When I test with showing exceptions I see
⨯ registration not active
---
• Tests\Feature\Http\Controllers\Auth\RegisterControllerTest > registration not active
Illuminate\View\ViewException
SQLSTATE[HY000]: General error: 1 no such table: settings (SQL: select * from "settings" where "feature" = register limit 1) (View: /Users/me/code/site.com/resources/views/auth/register.blade.php)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:759
755▕ // If an exception occurs when attempting to run a query, we'll format the error
756▕ // message to include the bindings with SQL, which will make this exception a
757▕ // lot more helpful to the developer instead of just the database's errors.
758▕ catch (Exception $e) {
➜ 759▕ throw new QueryException(
760▕ $query, $this->prepareBindings($bindings), $e
761▕ );
762▕ }
763▕ }
+14 vendor frames
15 app/Models/Setting.php:16
Illuminate\Database\Eloquent\Builder::first()
16 storage/framework/views/84de924690fd199ca5ce25f0291c19efdc00c175.php:5
App\Models\Setting::checkActive("register")
Tests: 1 failed, 5 passed
Time: 0.16s
But how can I then test for no form display without testing for database entries made to activate or not activate registration?
@rhand If the registration isn't enabled you need to check for 404 Not Found, 401 unauthorized, 403 Forbidden or any other code the describe the response, But not 50*.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
For the slow performance, check Lazy Database Refreshing
When more thing, if you have a common code that you need to run on every every test (like seeders), You can it that to setup method. In your test class, or if it's for every test in all test classes, you can add that setup method in your TestCase parent class.
https://phpunit.readthedocs.io/en/9.5/fixtures.html
Please or to participate in this conversation.