I am writing unit tests to validate a profile avatar module. So, I have a form that allows a user to upload an avatar. If one exists, it simply replaces the current one.
In my test, I do the following (the class setup logs a user in - not shown here):
f = open('testfile1.jpg')
data = {'image':f}
response = self.client.post('/profile/uploadavatar/',data)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.us开发者_如何学Cer1.get_profile().avatar.image.name, u'uploads/images/testfile1.jpg')
f.close()
f = open('testfile2.jpg')
data = {'image':f}
response = self.client.post('/profile/uploadavatar/',data)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.user1.get_profile().avatar.image.name, u'uploads/images/testfile2.jpg')
f.close()
The second assertEqual to test for avatar image name always fails because it is still set to the first filename (testfile1.jpg). However when I test this manually the code does what I think it should, which is replace the old avatar with the new one.
Am I doing something wrong? I'm new to the django unit tests so I may be missing something very simple...
Any ideas would be appreciated.
Thanks in advance!
The "self.user1" object, along with the profile, are cached at the beginning.
Reload the user/profile objects between actions to see updated data.
(Pulled up from the comments.)
精彩评论