1

我想测试用户注册,但我无法测试图像这是我的测试:

测试.py

response = self.client.post('/api/v1/signup/',
                                content_type='application/json',
                                data=json.dumps({"username": "casino", "email": "casinoluxwin@gmail.com",
                                                 "password1": "android12", "password2": "android12", "photo": {
                                                            'real_filename': "u'banner3.jpg'",
                                                            'path': "u'C:/Users/Dima/Desktop'"}

                                                 }))
    self.assertEqual(response.status_code, 200)

我得到代码 400(错误请求),但没有照片我的测试通过

service/users.py

 @validate_input({
    'username': {'min_length': 3, 'max_length': 50},
    'email': {'validation_type': "email", 'max_length': 50},
    'password1': {'min_length': 8, 'max_length': 50},
    'password2': {'min_length': 8, 'equal_to': 'password1',
                  'messages': {'equal_to': _(u"Пароли не совпадают")}},
    'photo': {'required': True}
})
@transaction.atomic
def signup(self, data):
    user_data = {
        'username': data['username'],
        'email': data['email'],
        'password': data['password1'],
        'coins_amount': 0
    }
    user = self._create_user(user_data)
    if data.get("photo"):
        self._attach_photo(user, data["photo"])

    obj, created = VerificationCode.objects.get_or_create(user=user, code_type="registration")
    obj.create_expiration_date()
    obj.create_code()
    obj.save()

    return user.id

所以我想测试用户照片,其他任何东西都可以正常工作。谢谢你的帮助

4

1 回答 1

0

Problem probably resides in either Users._attach_photo() or your user model. Not enough information here to decipher it entirely. There are a couple of things to try.

I'd write a normal unittest that does not use the client. It'll give you a more helpful traceback than just the HTTP status code from the running server. Something like:

    def test_user_add_method(self):
        x = Users.signup(json.dumps({"username": "casino", "email": "casinoluxwin@gmail.com",
                                             "password1": "android12", "password2": "android12", "photo": {
                                                        'real_filename': "u'banner3.jpg'",
                                                        'path': "u'C:/Users/Dima/Desktop'"})
    Users.get(pk=x) #will fail if user was not created.

Second, try commenting out your validator. 400 bad request could easily be kicked off by that. It is possible that your validator isn't playing nice with the image and you'll have to mess around with that.

于 2015-07-15T16:51:48.767 回答