You're not using the test **Client** at all. You're testing the value
of a form.Form instance.
The difference is that the test client constructs an actual request,
using the known URLs. So you'd need a view and url as well.
But these can exist only in the test and to be honest, especially
with uploads, I'm more interested in the upload working when used
in a view, then whether the form works in an environment it's never
going to be used in.
That said, James is right on the money with the payload. It doesn't
however tell you file uploads are going to work, only that a correctly
bound file passes validation.
On Fri, Sep 1, 2017 at 9:07 AM, Derek <gamesbook@gmail.com> wrote:
> Thanks Melvyn
>
> I thought my code did follow that approach - but it does not seem to work.
> So either I have not understood the docs (in which case, a point to the
> specific item is what I need) or there is some other factor at work.
>
> On 31 August 2017 at 16:47, Melvyn Sopacua <m.r.sopacua@gmail.com> wrote:
>>
>> In the documentation of the test client it is all spelled out how to test
>> file uploads.
>>
>> On Tue, Aug 29, 2017 at 8:48 PM, Derek <gamesbook@gmail.com> wrote:
>>>
>>> (Python 3.5 and Django 1.10)
>>>
>>> I am trying to test a Django form that enables a required file upload.
>>>
>>> The form is very simple and includes a field that looks like:
>>>
>>> upload_file = forms.FileField()
>>>
>>> The corresponding test to try and check the upload:
>>>
>>> def test_form_validation_with_file(self):
>>> fake_file = ContentFile(b'''Some file content''')
>>> fake_file.name = 'fake.xls'
>>> suf_file = SimpleUploadedFile('suf.xls', b'''this is text''')
>>> data = {
>>> 'upload_file': suf_file,
>>> }
>>> form = forms.RequestForm(data=data)
>>> self.assertTrue(form.is_valid())
>>>
>>> The form validation fails i.e. it does not seem to recognise that file
>>> data has been suppplied.
>>>
>>> I have also tried with the "fake_file" for the data but without success.
>>>
>>> Any help or pointers appreciated.
>>>
>>> Thanks
>>> Derek
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to django-users+unsubscribe@googlegroups.com.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAF1Wu3OTCbnw4o749YUz-Pa1-Uo9jkGkd1KybB5rSLnp3eAMDQ%40mail.gmail.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>>
>> --
>> Melvyn Sopacua
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/GUStj-3rzD8/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users+unsubscribe@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CA%2Bgw1GVt-pwLwdTx_0m2nj-uy0A%3DBQh8L1aeFNnraMCAy%3DP-iw%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAF1Wu3NR3Wqv56-i3xOo6QGrGU1Et5z3Kg-PM%2BGbBB6j5b2ZGw%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.
--
Melvyn Sopacua
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Bgw1GVCX8%2B%3DpLgtxq0ZiqSRohbigHTYsTCB3VtDLhSyj3b_%2BQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment