0

So, I've been trying to write unit tests for a function that returns a JSONResponse that contains a status_code and content. The function looks like this:

def exception_handler(request):
    return JSONResponse(
        status_code=status.HTTP_404_NOT_FOUND,
        content=jsonable_encoder({"detail": NotFoundException} if is_development() else {}),
    )

I'm testing for two cases: is_development() returns True or False. When I'm calling the function to test it like this: response = exception_handler(request) I can access response.status_code but not response.content. Why is that? I wanna access the content in order to check if it is correct.

Godzy
  • 49
  • 5

1 Answers1

0

I cannot really reproduce your problem. Here is the code which works in my case.

# ---- Some setup, to make your example work ----
from fastapi.encoders import jsonable_encoder

class status:
    HTTP_404_NOT_FOUND = 404

class JSONResponse:

    def __init__(self,  status_code, content):
        self.status_code = status_code
        self.content = content


# ----- Here your code -----
def exception_handler(request):
    return JSONResponse(
        status_code=status.HTTP_404_NOT_FOUND,
        content=jsonable_encoder({"detail": None} if is_development() else {}),
    )


def is_development():
    """True and False work"""
    return False


print(vars(exception_handler(None)))

Output:

{'status_code': 404, 'content': {}}

Could you provide more information?

Haller Patrick
  • 113
  • 1
  • 9