I'm new to writing tests and I have started with an application that's already running and I have a few questions:
I have my model Member with member data stored, that member has many Transaction and also many Testing (those are exams).
When writing unit tests for the member, on all tests I have to create the member, I see me repeating all the time some code.. Is this approach correct? Because when testing other parts maybe I have to create a member also...
Is there a place to put code and reuse it?
Maybe instead of creating and repeating the member I can use a method inside testing methods that return always the same member. Is this a good idea or practice?
Do you use comments inside your unit testing methods or on the method header?
My unit test for a specific feature is looking like this but I see that's no so reading friendly. I wanted to simplify this.
/**
* Member has pre paid testing corresponing with testing new rank?
*
* @return void
*/
public function test_has_pre_paid_testing_corresponding_with_new_testing_rank()
{
$member = factory(Member::class)->create(['id' => '1']);
$transaction = factory(Transaction::class)->create(['reason_id' => 10, 'member_id' => $member->id, 'school_id' => $member->school_id]);
$testing = factory(Testing::class)->create(['id' => '1', 'school_id' => $member->school_id]);
$testing->members()->attach($member->id);
$this->assertEquals(1, $member->id);
$this->assertEquals(10, $member->transactions()->first()->reason_id);
$this->assertEquals($member->school_id, $testing->school_id);
$this->assertEquals($member->id, $testing->members()->first()->id);
}