petritr's avatar
Level 15

unit test relations

I have complex relations, witch is the best way to test relations ? Any best practice examples ?

0 likes
5 replies
petritr's avatar
Level 15

@tykus i have this relations Users with Plan, Plan with Plan Assignment, Plan with Region, Region with program

It better do insert the relations in the factory, or do the test manually check the relations column ids? Or What do you recommend for unit tests ?

tykus's avatar

What do you want to test exactly; that they are hasOne type relations?

petritr's avatar
Level 15

Yes i need to check the relation between them HasOne or BelongsTo etc @tykus ?

tykus's avatar
tykus
Best Answer
Level 104

If you only wanted to test the relation, then it would be enough to test that the result of calling the relation method is an instance of the appropriate Relation, e.g.

$this->assertInstanceOf(\Illuminate\Database\Eloquent\Relations\HasOne::class, $plan->region());

If you want to know that the result of the relation is a particular class of Eloquent Model, then you would need to set up the relationship in your test database.

Please or to participate in this conversation.