0

I'm trying to make sure Testme.command() calls bar() on an instance of Dependency but I keep coming up short. I'm running this with python -m unittest tests.test_config and this code lives in tests/test_config.py in my project.

class Dependency():
    def bar(self):
        """ Do some expensive operation. """
        return "some really expensive method"


class Testme():
    def __init__(self):
        self.dep = Dependency()

    def command(self):
        try:
            self.dep.bar()
            return True
        except NotImplementedError:
            return False


import unittest
from unittest.mock import Mock


class TestTestme(unittest.TestCase):
    def test_command(self):
        with (unittest.mock.patch('tests.test_config.Dependency')) as d:
            d.bar.return_value = 'cheap'
            t = Testme()
            t.command()
            d.bar.assert_called_once_with()

When I run it, it fails like bar() never got called: AssertionError: Expected 'bar' to be called once. Called 0 times.

How should I test that Testme().command() calls Dependency().bar()?

1 Answers1

0

Try printing by doing

print self.dep.bar()

and

print d.bar.assert_called_once_with()

See if it outputs the correct value of "some really expensive method"

russOnXMaps
  • 159
  • 1
  • 10