lundi 4 janvier 2016

Proper testing flow with Laravel?

I am in the process of using Laravel to build an API for an IOS application. I am running into major issues testing the web app using PHPUnit and the built in Laravel Testing.

My process is 1. a user has an account 2. a user needs to be authenticated to perform any actions.

Lets say that I have groups of users, and that one user (the owner) can manage members that belong to one of their groups. I want to test the ability of a user to add other members to their group.

I have a testing method called testAddGroupMemberPass() which should

  1. create a user (owner)
  2. create a group and assign the owner
  3. create a user to add to the group
  4. assert that the owner has the ability to add that member.

    public function testAddGroupMemberPass()
    {
        // 1. create owner
        $owner = factory(User::class)->create();
        $token = JWTAuth::fromUser($owner);
    
        // 2. create group and attach user
        $group = new Group;
        $group->owner_id = $owner->id;
        $group->save();
    
        // 3. create the member to be
        $user = factory(User::class)->create();
    
        // 4. attempt attach the member to the group
        $this->post('/groups/' . $group->id . '/add-member/' . $user->username . '?token=' . $token)
        ->seeJson(['success' => true]);
    }
    
    

To assert that an owner can add a user you have to first create the owner, secondly create the group, thirdly create the user that you want to add to the group, and then finally you are able to try to make the request to the API to actually add that user.

The problem is that various issues can be encountered in the first 3 steps that are totally unrelated to the process of adding such as username and email unique constraints.

So what is wrong with my current approach? I've always been told that tests should be completely independent of one another so it wouldn't make sense to try and use users and groups generated by previous tests AFAIK.

Aucun commentaire:

Enregistrer un commentaire