1

I am trying to create unit tests for a private callback factory with an alarm manager class. I don't want to test the private method directly as I think this contravenes TDD good practice but I cannot figure out a good way to test this.

Please don't just tell me my code is bad (I already know!). I just want to know if it is possible to use the unittest framework to test this or if I need to re-architect my code.

My class is an alarm manager that is responsible for managing the creation, scheduling and sound of an alarm. When an alarm is created, it is passed to a scheduler with a callback function. To create this callback function, there is a private callback factory that takes in the alarm object to generate the custom callback (while capturing some variables from the manager class)

The problem I have is that unless I use the full functionality of the scheduler, the callback will never get executed but obviously there will be a lot of pain with patching time to make sure it executes in a reasonable time. Furthermore, the callback is never returned to the user so no easy way of checking (or even calling it there)

The manager class looks largely like the code below:

class Manager:

    def __init__(self):
        self._scheduler = alarm.scheduler.Scheduler()
        self._player = sound.player.Player()

    def create_alarm(self, name, new_alarm):
        self._scheduler.add_job(name, new_alarm.find_next_alarm(),\
            self._create_callback(name, new_alarm))

    def _create_callback(self, name, callback_alarm):
        def callback():
           self._player.play(callback_alarm.get_playback())
        return callback

Overall, is there a way to somehow extract the callback object if I make the scheduler a mock object. Or is there some other clever way to test that the callback is doing what it should be?

rmasp98
  • 97
  • 1
  • 6
  • "this contravenes TDD good practice" Why do you care? You are not doing TDD anyway. – Stop harming Monica Aug 29 '19 at 05:38
  • @Goyo do you think you could elaborate on that comment. Clearly I must be doing something incredibly wrong for you to conclude that I am not doing TDD from such little information – rmasp98 Sep 03 '19 at 20:16
  • Well apparently you have written code without tests and now you don't know how to test it. Had you write the test first (as in TDD) you wouldn't have panted yourself in that corner. – Stop harming Monica Sep 03 '19 at 22:12
  • The only code I have "written" before testing is the _create_callback function, which is purely there to help illustrate what I am trying to do. If it is so easy to design a test for ensuring the callback has been created properly, then why have you not answered the question... – rmasp98 Sep 04 '19 at 21:17
  • I can't answwer the question because I don't understand it. I don't understand how the tests that exercise `create_alarm()` can work before writing `_create_callback()`, since the former is calling the latter. I don't understand what "callback has been created properly" means. How would a client of `Manager` know if it has? – Stop harming Monica Sep 05 '19 at 06:15

0 Answers0