logo

How to enable Laravel Auditing during tests

And why I needed to do it

Joel Clermont
Joel Clermont
2025-11-20

I was recently working through an issue where we were using the Laravel Auditing package along with the Laravel Ciphersweet package for encryption. I needed to enforce that any encrypted fields were being audited correctly, and that the audit logs were not inadvertently storing plaintext data for those fields.

Something this sensitive and critical meant that I was definitely going to write tests to verify the behavior.

The very first, most basic test was failing though. The audit records I was trying to assert against were not being created as expected.

After a little digging, I discovered that the auditing package is configured by default to disable auditing when code is running in the console.

This makes sense, since you probably wouldn't want to generate a ton of audit logs when seeding the database.

But tests also run through the console, so this was why I wasn't seeing any audit records get created.

The solution was simple: just alter the config inside my test to turn auditing back on for console runs.

Config::set('audit.console', true);

This is a targeted approach and only affects the config for just this one test, not your whole application.

With this one line in place, my tests now passed as expected.

Here to help,

Joel

P.S. Testing is a topic we discuss quite a bit in the Mastering Laravel community. Join us and ask your testing questions.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.