我正在为 Django View 编写单元测试,我的单元测试中有这个:
data_from_paypal_other_id = {'client_id': '1', 'receiver_email': 'badguy@example.com', 'amount': '1.5'}
self.client.post(reverse('paypal_ipn_listener'), data=data_from_paypal_other_id)
问题是 PayPal 要求我以特定顺序发送带有参数的 POST 请求。 Don't ask me why, see this .
我看到 Django 测试客户端的参数顺序乱七八糟:
--BoUnDaRyStRiNg
Content-Disposition: form-data; name="receiver_email"
badguy@example.com
--BoUnDaRyStRiNg
Content-Disposition: form-data; name="amount"
1.5
--BoUnDaRyStRiNg
Content-Disposition: form-data; name="client_id"
1
--BoUnDaRyStRiNg--
注意 receiver_email
在 client_id
之前跳转。
我的问题是如何强制 Django 测试客户端保持参数的顺序?
请您参考如下方法:
使用 collections.OrderedDict
而不是 dict
应该可以解决问题。确保使用序列而不是原始字典对其进行初始化,例如:
from collections import OrderedDict
data_from_paypal_other_id = OrderedDict([
('client_id', '1'), ('receiver_email', 'badguy@example.com'),
('amount', '1.5')])