[ErrorCatcher 2] Django UnitTests

LILO Ghim·2021년 12월 17일
0

unitTests시에 발생할 수 있는 error & 실수

post view 작성 후 진행한 UnitTests 실행 중 만났던 다양한 error와 크고 작은 실수들의 기록


response = client.post('/users/host', json.dumps(host), content_type='application/json', **headers)

front-end 에서 보내는 data(headers, body) 그대로 input을 한다고 가정하고 작성하는 것이 기본!

무엇이 input이고 무엇이 oup인지 정확하게 작성!


def setUp

1-1) one to many table

해당하는 view에서 import 하는 모델에서 foreign key를 가지고 있는 컬럼의 테이블이 있는 경우, 순서대로 data를 (실제로 등록한다고 가정하고!)작성해서 저장한 후, 해당 view의 data 값에 User.objects.get(id=1)과 같이 data를 생성해 준다.

1-2) column

  raise self.model.DoesNotExist(
  categories.models.Category.DoesNotExist: Category matching query does not exist.  

database에 저장되는 컬럼명 그대로 작성 등록할 데이터를 보낼 때도, Http나 postman에서 보내는 input data와 같이 그대로 작성하여 test!

decorator

AssertionError: {'message': 'INVALID_TOKEN'} != {'message': 'Registered Category'}
- {'message': 'INVALID_TOKEN'}
+ {'message': 'Registered Category'}

socail login unittest에서는 가짜 access_token을 보냈지만, signin_decorator를 사용하고 있는 hostview에서는 실제 token을 보내서 사용자를 인가한다.
setup에서 등록한 db의 user_id를 사용해서 token 발급함

access_token = jwt.encode({'id': 1}, SECRET_KEY, ALGORITHM)

headers = {"HTTP_Authorization" : access_token}

response = client.post('/users/host', json.dumps(host), content_type='application/json', **headers)

return message

AssertionError: {'result': 'Resisterd Category'} != {'message': 'Registered Category'}
- {'result': 'Resisterd Category'}
?   ^  ^^^      ^

+ {'message': 'Registered Category'}
?   ^  ^^^^      ^     +

view에서 작성한 return message와 unittest의 return message를 다르게 작성하여 발생한 에러

profile
킴릴로

0개의 댓글