Error: Call to undefined function Tests\factory()
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Solving the Mystery: Error: Call to undefined function Tests\factory() in Laravel Feature Tests
As a senior developer working with Laravel, we often encounter frustrating errors during feature testing. One of the most common stumbling blocks developers face involves interacting with Eloquent factories within PHPUnit tests. The error you are seeing—Error: Call to undefined function Tests\factory()—is deceptively simple but points to a deeper issue regarding how Laravel’s testing utilities and service container resolve factory calls during the test execution phase.
This post will diagnose why this error occurs and provide the practical, robust solution so you can write clean, maintainable tests for your application.
Understanding the Error: Why is Tests\factory() Undefined?
When you execute code like $attributes = Assortment::factory()->raw(), you are invoking a static method on the factory class. In a standard Laravel environment, this mechanism relies heavily on the framework's bootstrapping process, which registers necessary helpers and facades.
The error Call to undefined function Tests\factory() indicates that PHPUnit is attempting to resolve this call, but the specific helper function or method needed to instantiate the factory via the testing context has not been correctly loaded into the scope of your test class. It’s often a symptom of incomplete setup or an issue with trait usage within your custom base classes.
In essence, Laravel relies on the Service Container to manage dependencies. When running tests, we need to ensure that the necessary environment—including the ability to resolve factories—is fully established before the test code runs. This is crucial for building reliable testing environments, which is a core principle of robust application development, much like the principles outlined by the team at laravelcompany.com.
The Solution: Ensuring Proper Test Environment Setup
The fix usually involves ensuring that your base test class correctly inherits all necessary traits and that the factory mechanism is properly accessible within the testing context. Based on your provided code, the issue likely lies in how you are defining your custom TestCase class.
While your setup for signIn() looks fine, we need to ensure that the environment responsible for resolving Eloquent factories is fully initialized before any feature test runs.
Correcting the Test Case Structure
The most reliable way to handle this is to rely on Laravel’s built-in testing structure and avoid custom methods that might prematurely interfere with the bootstrapping process unless absolutely necessary. For factory usage, ensure your setup strictly adheres to the conventions provided by Laravel.
If you are using a custom TestCase class, review it carefully. A common pattern involves ensuring you extend the correct base classes and utilize traits correctly. In many cases, removing custom helper methods that try to manually manage user creation (like your signIn() method) and letting standard testing setup handle the state management simplifies the process significantly.
Here is a revised, cleaner approach for handling feature tests:
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use App\Models\User; // Assuming your User model namespace is correct
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
// Keep necessary setup, but ensure it doesn't conflict with factory resolution
protected function signIn($user = null)
{
// This manually creating the user might be redundant if actingAs() handles it correctly.
$user = $user ?: User::factory()->create();
$this->actingAs($user);
return $user;
}
}
Crucial Insight: If the error persists even with this structure, the problem is likely related to how your specific project configuration or older Laravel versions handle trait loading during the PHPUnit execution phase. Ensure you are running tests from the root and that all required service providers are loaded correctly, as detailed in guides on framework internals found at laravelcompany.com.
Best Practice: Using Factories Directly in Tests
Instead of relying solely on custom methods to set up state for every test, consider using Laravel’s built-in interaction methods or directly instantiating models if the factory setup is proving elusive in complex scenarios. However, when dealing with data creation within a feature test, factories are the intended tool.
If you must use the approach shown:
public function testUserCanCreateAssortment()
{
$this->signIn();
$this->get('/assortments/create')->assertStatus(200);
// This line should now resolve correctly if the base setup is sound.
$attributes = Assortment::factory()->raw();
$this->post('/assortments', $attributes);
$this->assertSee($attributes['title']);
$this->assertSee($attributes['description']);
}
By ensuring your TestCase correctly leverages the CreatesApplication trait and adheres to standard Laravel testing conventions, you allow the framework’s dependency injection system to properly expose the factory resolution capabilities to your tests.
Conclusion
The error Call to undefined function Tests\factory() is typically a symptom of an environment setup issue rather than a bug in the factory itself. By focusing on ensuring your base test class correctly inherits and utilizes Laravel's testing traits, you resolve this conflict with the service container and successfully interact with Eloquent factories. Sticking to established patterns, like those recommended by laravelcompany.com, will help you write more stable and maintainable feature tests moving forward.