python mock - 补丁装饰器不起作用(再次)

时间:2016-06-28 15:17:28

标签: python django unit-testing mocking

问题:我试图用硒写测试。在用户操作期间,站点可以执行与其他服务的通信,我尝试模拟这些部分。 代码:

accounts.views.py

class RegistrationView(GuestMixin, FormView):
    template_name = 'registration.jinja'
    form_class = RegisterForm

    def get_context_data(self, **kwargs):
        return super(RegistrationView, self).get_context_data(**kwargs)

    def form_valid(self, form):
        return JsonResponse(form.save(self.request), safe=False)

    def form_invalid(self, form):
        return JsonResponse({'errors': form.errors.as_json()}, safe=False)

accounts.forms.py(类似这样)

class RegisterForm(forms.Form):

    # some fields...

    class Meta:
        model = User

    def save(self, request):

        activated = not settings.REQUIRE_SMS
        user = User.objects.create_user(...)

        user = authenticate(username=self.cleaned_data.get('tel'), password=self.cleaned_data.get('password'))

        login(request, user)

        if settings.REQUIRE_SMS:
            # XXX kaboom
            user.send_activation()
            Redis.save_user(user)

accounts.models.py

class User(AbstractBaseUser, PermissionsMixin):

   # fields...

    def send_activation(self):

        gateway = sms.SMSGateway()
        sms_code = random.randrange(1000, 9999)

        if gateway.send(self.tel, sms_code):
            self.sms_code = sms_code
            self.code_expiries = now()
            self.is_active = False
            self.save()

            return True
        else:
            return False

accounts.tests.test_selenium.py

class LoginSeleniumTest(StaticLiveServerTestCase):

    # other methods and stuff...

    @patch(u'accounts.forms.User.send_activation')
    def test_registration_no_name(self, mock_send_activation):

        mocked_gateway.return_value = True

        url = reverse('user:registration')
        self.browser.get('%s%s' % (self.live_server_url, url))

        form = self.get_registration_form(username=self.username, email=self.email, phone=self.phone, password=self.password)
        form.click()
        time.sleep(3)

        name_error = self.browser.find_element_by_css_selector('div#register-name > .error.animate')
        self.assertEqual(name_error.text, u'Обязательное поле.')
        self.assertEqual(User.objects.count(), 0)

问题是在#XXX kaboom中,user.send_activation方法仍未被取消,我无法理解如何制作它。我对此感到疯狂。

0 个答案:

没有答案