6 months ago

Testing Notifications

Posted 6 months ago by bwrigley

I have set up my project so that a user can specify if they would like their notifications instantly, daily, weekly and via sms, email, or just in their dashboard.

So if a user has said 'instantly' then I fire off an sms/email right away.

If they have said daily/weekly, then I run a job with the Scheduler which checks (at the right time) if a user has any unread notifications and send them an email/sms alerting them to how many unread notifications they have.

All good. Now I want to write a test around this.

This means I need to seed the database with some notifications against a user's account, but I can't figure out a good, clean, reproducible way to do this. I'm sure I'm just being dumb. The options I've thought of are:

  1. In my test, create a number of instances of an existing Notification class. This feels wrong as my test shouldn't expect the existence of a certain Notification class to run.
  2. Create a TestNotification class. This is doable, but seems messy and under which namespace would it be stored? Along with the other Notification classes?
  3. Seed the database directly with raw queries. Again feels messy not to use the mechanisms that are built-in for notifications, and what would I store in the type field?
  4. Write a factory around the Notification model. I tried this but couldn't get it to work, but maybe this is the correct way?

I would really appreciate your thoughts.



Please sign in or create an account to participate in this conversation.