alchermd's avatar

If you write a test for an Eloquent model that contains business logic, is it still a Feature or a Unit test?

Hola!

As an example, this Laracasts episode features a Team class that can add/remove members. The gist of the class is as follows:

class Team extends Model
{
    public function add(User $user) : void;
    public function remove(User $user) : void;
    public function count() : int;
    // etc...
}

The tests are as follows:

class TeamTest extends TestCase
{
    public function it_can_add_members() { }
    public function it_enforces_a_maximum_size() { } 
    public function it_can_remove_members() { } 
}

I have some questions:

  1. Is this test considered a feature test since it involves multiple classes (Team and User)

  2. If it isn't, does the fact that it hits the database (it being an Eloquent model) makes it not a unit test?

  3. Is the Team class the correct place to add the business logic in this case?

Thanks a bunch!

0 likes
1 reply
lostdreamer_nl's avatar

As I've always understood it: it's a unit test when you are testing a single unit ( / class).

You are only testing the Team object, so imo, it's a unit test.

You are saying 'it hits the database' so what? It doesnt have to as you are only testing that, when you add a user to a team, it is in the team. The test doesnt check the database for that, it asks the team object (which could for instance just be writing ID's to some text file)

As long as you only interact with the Team object from within your test, I believe this to be a unit test.

As soon as you try to hit the website, filling in a form, and then testing if a team has a certain user in it, or if you use the Team object to add a user, and then you check the DB if it has the right data in it, at that point you are actually testing if Team can work together with Database (or Frontend) which would make it a feature test.

1 like

Please or to participate in this conversation.