New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Added WithFaker testing trait #22280

Merged
merged 1 commit into from Dec 4, 2017

Conversation

Projects
None yet
3 participants
@vinterskogen
Copy link
Contributor

vinterskogen commented Dec 2, 2017

This PR contains a small helper trait for your tests: WithFaker.

WithFaker trait adds a faker instance to your test class, that helps you generate any fake data in your tests in manner like in an example below.

This trait is optional, of course. You can pull it in tests, where you plan to generate a bunch of dummy data (to fake user input or so), especially when this data has a variety of locales.

Sticking with this approach is more convenient than instantiating a faker object manually in each test, as well as doing the same thing in setUp method.

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    use WithFaker,
        RefreshDatabase;

    /**
     * With faker test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        // Generate value of a default locale ('en_US').
        $americanName = $this->faker()->name;

        // Generate value of a specified locale.
        $russianName = $this->faker('ru_RU')->name;

        // Get the default faker instance for a given locale (for manual use in test).
        $faker = $this->faker();
        $faker = $this->faker('ru_RU');

        // Or access faker instance directly via a class property.
        $this->faker->url;
    }
}

@vinterskogen vinterskogen changed the title Added WithFaker testing trait [5.5] Added WithFaker testing trait Dec 2, 2017

@jordyvandomselaar

This comment has been minimized.

Copy link

jordyvandomselaar commented Dec 2, 2017

Aren't you supposed to use factories inside tests? Iirc factories can access faker.

@vinterskogen

This comment has been minimized.

Copy link
Contributor Author

vinterskogen commented Dec 2, 2017

You mean model factories? Yes, they are usefull for seeding initail state of database tables while testing. But often generation fake input data is not all about retriving an array of attributes (raw()) from а model factory. Especially in case of HTTP tests. Here comes the faker. ;)

@taylorotwell

This comment has been minimized.

Copy link
Member

taylorotwell commented Dec 4, 2017

Is there any problem with just always having Faker available to tests in Laravel 5.6, without having to opt-in via a trait?

@taylorotwell taylorotwell merged commit 8eb6348 into laravel:5.5 Dec 4, 2017

2 checks passed

continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@vinterskogen

This comment has been minimized.

Copy link
Contributor Author

vinterskogen commented Dec 4, 2017

The idea of always having Faker available to tests is good. The only possible problem about it, as I see by my self, is if PHPUnit's 'PHPUnit\Framework\TestCase' class will suddenly define either a "faker" method or a property in its future releases. It will cause a name collision then. But since S.Bergmann had not added something like that into PHPUnit yet, it looks safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment