Reputation: 21
I am writing Test Cases for API using Django framework, the GET method seems to be working all right but when it comes to POST error message always shows up:
'dict' object has no attribute 'data'.
Exact error is,
res = respo.post({'ticker': 'FIB','open': 7.0,'close':8.0,'volume':200}) File "C:\Users\sathya.m\Desktop\mydsite\companies\views.py", line 32, in post serializer = StockSerializer(data=request.data) AttributeError: 'dict' object has no attribute 'data'
views.py
def post(self,request):
serializer = StockSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=200)
tests.py
def test_getDataDetails(self):
respo = StockList()
resp = respo.get({'username':'admin','password':'pass'})
res = respo.post({'ticker': 'FIB','open': 7.0,'close':8.0,'volume':200})
self.assertEqual(res.status_code,200)
Upvotes: 1
Views: 6117
Reputation: 21
Post is working fine, by using this below code:
def test_getUserDetails(self):
url = '/user/'
data = {"username": "Ramu", "first_name": "Ram", "last_name": "Ram", "email": "[email protected]"}
headers = {'Content-Type': 'application/json'}
r = requests.post(url, data=json.dumps(data), headers=headers)
self.assertEqual(r.status_code,201)
Upvotes: 1
Reputation: 77902
Your view's get and post methods expect a request object as argument, not a dict. You either have to provide this request object yourself or use django's test client (cf the part about testing in the fine manual)
Upvotes: 1