Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

steve_laracasts's avatar

Spark TDD

Hey everyone, I am slowly working my way through learning TDD and doing okay, except that I am using Spark and having issues following the documentation with this kind of declaration in my tests:

$this->actingAs(factory('App\User')->create());

This works, it creates the user no problem, but the test I am writing fails (I think!) because there's a check in Spark that stops a user from doing anything else until they join a Team! There is certainly a redirect that interferes with where I want to go next - if that makes sense?

So, my question is, what is the best way to go about creating a factory to first generate a user, then a team and finally assign the created user to that team. Does anyone have any code they have already written to overcome this issue they are willing to share please?

I suspect this is something that is going to get used a lot, so I am hoping someone can point me in the right direction as I can kinda see how to do it, but I am also quite new at this and any input that would expedite my progress would be very gratefully received.

Thank you!

As an aside, I would love to see all the tests that were written as part of the development for Spark - I think that would be very educational for me :)

0 likes
7 replies
martinbean's avatar

@kel_ You could create a trait that contains a method for creating a user with a team attached:

namespace Tests\Feature\Concerns;

use App\Team;
use App\User;

trait CreatesUsers
{
    protected function createUser()
    {
        $user = factory(User::class)->create();
        $team = factory(Team::class)->create();
        // Attach user to team
        return $user;
    }
}
class SomeTest extends TestCase
{
    use CreatesUsers;

    public function someTestMethod()
    {
        $this->actingAs($this->createUser());

        //
    }
}
steve_laracasts's avatar

Oh wow... thank you - I think I was sooooooooo close!!

I got distracted as I had to fix a eurorack module for someone; I've got it working again, just got to package it up and get it sent off then I will return to carry on with coding and check this out to see where I went wrong.

I think I also have to read up some more on traits.

I am quite pleased with myself that I managed to get so far with this solution, and really appreciate your taking the time to help me - thank you once again :)

I might be back for further assistance, but if I get it working I will post the actual code I end up using here too.

steve_laracasts's avatar

Okay I am confused and from searching for an answer it seems I am not the only one, yet I have't been able to find an answer as yet. I am just trying to get the Team factory working before I start and write any further code, it seems this should be simple, I already have another factory called ProjectFactory that is working fine!

/database/factories/TeamFactory.php

<?php
use Faker\Generator as Faker;
$factory->define(App\Team::class, function (Faker $faker) {
    return [
        'owner_id' => 1,
        'name' => $faker->name,
    ];
});

Yet in Tinker I get:

>>> factory('\App\Team')->create();
InvalidArgumentException with message 'Unable to locate factory with name [default] [/App/Team].'

I know this is almost certainly my fault - what am I missing please?

Thank you!

steve_laracasts's avatar

Found it - eventually:

factory('App\Team')->create();

NOT

factory('\App\Team')->create();
steve_laracasts's avatar

I have finally got this all working, I need to refactor as it is not pretty, but essentially what @martinbean said above, but in my own crappy way (hence the need for refactoring).

Essentially to perform any authenticated user test with Spark, as well as a user you must also create a team and a relation between the team and the user in the team_users database table.

This gets the job done:

DB::update('update users set current_team_id = ' . $team_id . ' where id = ?', [$user->id]);
DB::insert('insert into team_users (team_id, user_id, role) values (?, ?, ?)', [$team_id, $user->id, 'anything']);

I ended up doing this manually using the above DB queries and it works fine, but always happy to learn more about the Laravel way, I would be delighted if someone with more experience than myself would be able to teach me something?

Thanks again to everyone who helped and everyone who took the time to read my post :)

Kind regards

steve_laracasts's avatar

Just found this:

$user = factory(User::class)->create();
$team = $this->createTeam($user, 'member');

Got to try this very soon, this is much better :)

Please or to participate in this conversation.