jeudi 31 mai 2018

how mock allows checking the arguments sent to the mocked function in django testing

This is the method where i want to mock send_email()

 def send_set_password_email(self):
    self.update_hash()
    url = self.user.password_set_url()
    recipient = self.user.email
    context_dict_map = {}
    context_dict_map[recipient] = Context(dict(
        partner_realtor_obj=self,
        full_name=self.contact_name,
        change_password_url=url,
        slug=self.slug,
        host = "http://%s.moveeasy.com/" % (self.slug, ),
        STATIC_URL = settings.EMAIL_STATIC_URL
    ))
    if self.brokerage_link:
        subject = '%s has empowered you with MoveEasy' % (self.brokerage_link.name)
    else:
        subject = 'You have been empowered with MoveEasy'
    send_mail(
        recipients=[recipient], subject=subject,
        text_content='Set your password at the following url: %s' % (url,),
        template_name='emails_table/realtors/set-password.html',
        context_dict_map=context_dict_map,
        subdomain=self.slug, email_type='Realtor: Set password'
    )

In my testcase i have mocked it like

@mock.patch('moving_concierge.utils.send_mail')
def test_send_set_password_email(self, send_mail):
    image_content = open(
        os.path.join(settings.BASE_DIR, 'static/images/avatar.jpg'), "rb"
    ).read()
    logo = SimpleUploadedFile('image.jpg', image_content)
    user = User.objects.create_realtor(
        email='john@doe.com', password='Abc123!'
    )
    realtor = RealtorDetails.objects.create(
        user=user, slug='john-doe',
        logo=logo, hash='j12h9on8'
    )
    realtor.send_set_password_email()
    url = realtor.user.password_set_url()
    send_mail.returnvalue = context_dict_map

I need to Assert that proper context_dict_map is passed to send_mail, How can i do that ???

Please help me out guys, Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire