Functionally test controller method without hitting DB

I am looking for the proper way to functionally test a controller method that is currently using Lucid directly in the method.

Psuedo-example:

show({request}) {
  return SomeModel.first();
}

If I want to test this method without hitting the DB would the best approach be to extract the model out into a Repository and mock/fake that or can I mock Lucid directly? Is there a best practice here?

Controller job is to take the request data and process it in some way. Which is saving it to the database in your case.

If you fake the database call, what are you testing then?

1 Like

If there is some logic after fetching of data through a Lucid model is more what I was thinking. Something like:

show({request}) {
  const something = await SomeModel.first();
  // Do some logic here
  return processedResults;
}

If i want to test this entire route functionally but I don’t want to hit the database is there a best practice for faking Lucid in this scenario?

What’s the benefit of not hitting the database?

I believe mocking should be last option in any sort of testing

1 Like