I usually don't test relationships in a unit test but rather in a feature or end to end test. The only thing I usually test in my models is the set and get attributes and any other methods that is no relationships.
Unit Testing a Model Class
Hi, do you guys do a unit test on your model class? like if you have a unit test on your service and repository class and you also have a feature test on your controller. do you still need test on your models?
@kevdev I seldom unit test model classes as many model methods interact with a database, and a unit test shouldn’t interact with external services like a database.
For model methods, I’ll usually create a separate Integration directory and namespace in my tests directory, and test method methods in there. I won’t go to the point where I’m testing relationship methods though, because that’s not really testing anything useful. Eloquent will already have tests for relationship methods.
Testing a has-many relation method returns a HasMany instance just isn’t that helpful to me. Something else is going to break if I change that to a HasOne or something…
Please or to participate in this conversation.