xtremer360
4 months ago

Data Providers Are They Worth It

Posted 4 months ago by xtremer360

I am trying to decide if using data providers for my tests is the best way to go.

Below I have an example of a test where an administrator can update an inactive user to be active. I have a data provider that I can apply to this test that returns an array of two different types of administrator roles. This array of roles are roles that have the ability to perform this action.

I understand for this simple test it would be just as easy to create a new test that assures the same functionality passes for the two different admin types however this starts to get bigger as different conditions are met on whether or not the user can be activated at all.

On the other end of the spectrum, I have 20+ tests that feature endpoint testing that tests form validation fails with passing the post parameters. Those tests all say that a specific role is logged in during those tests but both types of admin roles have the same type of results.

I'm seeking advice as to if many people reach for data providers a lot with situations like this or not or if there are some better alternatives. I've heard that some people will separate out the same tests into separate directories with a directory for each role but seems like a copy and paste for the same test with one minor change. Plus when criteria changes or another role is added you'd have to go in and create an additional directory with more copied tests.

What do you all think?

/** @test */
    public function an_administrator_can_activate_an_inactive_user()
    {
        $this->actAs('administrator');
        $user = factory(User::class)->states('inactive')->create();

        $response = $this->put(route('users.activate', $user));

        $response->assertRedirect(route('users.index'));
        tap($user->fresh(), function ($user) {
            $this->assertTrue($user->is_active);
        });
    }

Please sign in or create an account to participate in this conversation.