Summer Sale! All accounts are 50% off this week.

Sofia's avatar
Level 6

Looking for some testing advice

Hello,

I'm trying to refactor a function that does a complex transformation so that can be tested without having to access the database.

The function accepts an associative array that is constructed from some form input and returns a collection of models. There are a couple of eloquent queries that fetch data required for the transformation, and this is the part that I need to factor out, but it's tangled up at the moment. The query parameters are based on input.

Ideally, we could test this transformation as a 'unit' vs. breaking it up into several calculations (before and after queries). Breaking it up would force us to create many more tests to cover all the cases.

I was also thinking about creating a repository for the queries, where it can be swapped for one that returns in-memory models when testing, but not sure how to put that together and haven't been able to find any good examples online.

Any ideas, relevant resources, or advice appreciated.

Thank you!

0 likes
2 replies
Sofia's avatar
Level 6

@webrobert Yes! I watched that not too long ago. That's where I was going with the repository option, but still wasn't sure how to implement it in our case. Would that be a reasonable approach? Are there any casts or examples of mocking a repository in particular?

Please or to participate in this conversation.