Laravel models in testing

Wednesday, November 4, 2015

Unfortunately I'm fairly new to testing, I get the basics of it, but I want to delve a bit deeper and test a Laravel Model.



I've created a model factory which creates a new User model, and populates it with data.



The User model has a lot of relations - Roles, Flags, Notes etc. Some of these relations are required.



In my tests, I can create a new instance of the User model with the factory() function call.



My question is:



Do I need to create a new instance of the User object, as well as all required relations within each test? Or can I just create the user object and all related records in a constructor (or setUp() method?)



It seems a little pointless to constantly create user objects and all relations - especially if the a base model has a lot of relations - in every test method, but I cant find anything in the Laravel documentation about the best practice for this.



Can anyone shed any light on this?

0 comments:

Post a Comment