title: Mock a model method in Django
body: <p>I'm just getting started with <code>Mock</code> for testing Django apps without touching the db. I can successfully mock my model objects in my tests, but I'm a little confused as to how to deal with model methods which involve objects.</p>
  
  <p>As an example I have this in my models.py:</p>
  
  <pre><code>class Skill(models.Model):
     title = models.CharField(max_length=255)
     category = models.ForeignKey(
         SkillCategory, default=None, null=True, blank=True
     )
  
  def __unicode__(self):
      return self.title
  
  class UserProfile(models.Model):
      user = models.OneToOneField(User)
      skill = models.ManyToManyField(Skill, default=None)
      avatar = models.URLField(
          max_length=400, default=None, null=True, blank=True
      )
  
      def __unicode__(self):
          return self.user.username
  
      def update_skills(self, cleaned_skills):
          for s in cleaned_skills:
              skill, created = Skill.objects.get_or_create(title=s)
              self.skill.add(skill)
  </code></pre>
  
  <p>So far in my re-factored tests I have done this:</p>
  
  <pre><code>class ProfileTestCase(unittest.TestCase):
      def setUp(self):
          # mock user
          self.user = mock.Mock(spec=User)
          self.user.username = "john"
          self.user.email = "johndoe@hotmail.com"
          self.user.password = "pass"
          # mock profile
          self.userprofile = mock.Mock(spec=UserProfile)
          self.userprofile.user = self.user
          # mock skill category
          self.skill_category = mock.Mock(spec=SkillCategory)
          self.skill_category.title = "foo"
          # mock skill
          self.skill = mock.Mock(spec=Skill)
          self.skill.title = "ninja"
          self.skill.category = self.skill_category
  
      def test_skillcategory_created(self):
          self.assertEqual(self.skill.category.title, 'foo')
  
      def test_skill_created(self):
          self.assertEqual(self.skill.title, 'ninja')
  
      ...
      # and so on
  </code></pre>
  
  <p>In my old tests, which use the db, I had this to test my model method:</p>
  
  <pre><code>    ...
  
      def test_update_skills(self):
          cleaned_skills = ['mysql', 'unix']
          self.userprofile.update_skills(cleaned_skills)
          skills = self.userprofile.skill.values_list('title', flat=True)
          self.assertTrue('mysql' in skills)
  </code></pre>
  
  <p>I'm scratching my head on how I should refactor this line:</p>
  
  <pre><code>self.userprofile.update_skills(cleaned_skills)
  </code></pre>
  
  <p>to take advantage of <code>Mock</code>.</p>