inertia
inertia

Reputation: 404

Prevent calling actual function when using mock.MagicMock

I have a model:

class Evaluation(models.Model):
   def abc(self):
      #some logic here
      return something

Say, I have another function which uses this:

def filter_graded(evaluations):
    return filter(lambda x: x.is_completely_graded(), evaluations)

Now when I am writing test for filter_graded method, I mock is_completely_graded to return true/false for some instances. Instead of respecting the mock, the test calls actual is_completely_graded. How can I solve this?

My test looks like:

def test_filter_graded(self):
    eval1 = G(Evaluation)
    eval2 = G(Evaluation)
    eval1.is_completely_graded = mock.MagicMock(return_value=True)
    eval2.is_completely_graded = mock.MagicMock(return_value=False)
    filter_graded(Evaluation.objects.all())
    self.assertEqual(len(result), 1)

Upvotes: -1

Views: 1112

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1125078

You are not passing in mocked objects. You are passing in real data:

filter_graded(Evaluation.objects.all())

Nowhere did you mock out Evaluation or Evaluation.objects or Evaluation.objects.all().

If all your code does is look for objects with a is_completely_graded method, just pass in mocks as the sequence, no need to any ORM queries:

def test_filter_graded(self):
    eval1 = mock.MagicMock()
    eval2 = mock.MagicMock
    eval1.is_completely_graded.return_value = True
    eval2.is_completely_graded.return_value = False
    filter_graded([eval1, eval2])
    self.assertEqual(len(result), 1)

Upvotes: 1

Related Questions