LARAVEL Quality Control & Assurance 4 — Questions and Answers
Question 1: What does `$this->actingAs($user, 'api')` do in a Laravel test?
- Authenticates the user via the api guard for the test request (Correct answer)
- Creates a Sanctum token for the user
- Generates a JWT for the user
- Switches the default guard to api permanently
Correct answer: Authenticates the user via the api guard for the test request
actingAs accepts an optional guard name so the test authenticates through that specific guard rather than the default web guard.
Question 2: Which Laravel testing helper checks that a Blade view was rendered with specific data in a unit test?
- $this->view('view.name', $data)->assertSee() (Correct answer)
- view()->assertRenderedWith()
- $this->assertView()
- View::assertData()
Correct answer: $this->view('view.name', $data)->assertSee()
The view() test helper returns a TestView instance on which you can call assertSee, assertViewHas, and similar assertions.
Question 3: In Laravel, which trait must be added to a test class to use model factories with the `create()` method via database interaction?
- RefreshDatabase (Correct answer)
- WithFaker
- WithoutMiddleware
- DatabaseMigrations
Correct answer: RefreshDatabase
RefreshDatabase (or DatabaseMigrations/DatabaseTransactions) ensures the schema is available so factory create() can persist records.
Question 4: Which Dusk method fills an input field identified by its name attribute?
- type (Correct answer)
- fill
- input
- setValue
Correct answer: type
Browser::type('field-name', 'value') locates an input by name and types the given value into it.
Question 5: What is the role of `Storage::fake('local')` in Laravel tests?
- Replaces the local disk with an in-memory filesystem so no real files are written (Correct answer)
- Mocks all disk method calls and returns null
- Disables file upload validation
- Forces the disk to use the public directory
Correct answer: Replaces the local disk with an in-memory filesystem so no real files are written
Storage::fake creates a temporary local filesystem so tests can assert file operations without touching the real disk.
Question 6: Which PHPStan/Larastan level enforces the strictest type checks in a Laravel project?
- Level 9 (Correct answer)
- Level 5
- Level 7
- Level MAX
Correct answer: Level 9
PHPStan level 9 is the highest standard level, checking the most type-safety rules including dead code and mixed type usage.
Question 7: Which Laravel command runs the test suite and displays code coverage as a percentage in the terminal?
- php artisan test --coverage (Correct answer)
- php artisan test --report
- php artisan test --with-coverage
- php artisan test --clover
Correct answer: php artisan test --coverage
php artisan test --coverage outputs a coverage summary in the terminal using Xdebug or PCOV.
What does `$this->actingAs($user, 'api')` do in a Laravel test?