英文:
Getting the User from an APIClient() in DRF
问题
以下是您要翻译的代码部分:
I'm using a fixture in pytest that returns a client that has been logged in:
@pytest.fixture
def create_client(create_user) -> APIClient:
data = {
"email": create_user.email,
"password": "TestPassword",
}
client = APIClient()
client.post(path="/user/login/", data=data)
return client
How I get the user that has been logged in in the test?
I understand that I might be able to use a get a request from the client and get it that way:
def test_get_user(create_client):
response = create_client.get(path="/some/random/path/")
user = response.user
return user
but is there a better way to do this?
英文:
I'm using a fixture in pytest that returns a client that has been logged in:
@pytest.fixture
def create_client(create_user) -> APIClient:
data = {
"email": create_user.email,
"password": "TestPassword",
}
client = APIClient()
client.post(path="/user/login/", data=data)
return client
How I get the user that has been logged in in the test?
I understand that I might be able to use a get a request from the client and get it that way:
def test_get_user(create_client):
response = create_client.get(path="/some/random/path/")
user = response.user
return user
but is there a better way to do this?
答案1
得分: 1
I'm sure that the create_user
is another fixture and thus you can have that in your test_get_user(...)
as
<pre><code>
def test_get_user(create_client, create_user):
print(create_user)
# do some tests here
</code></pre>
英文:
I'm sure that the create_user
is another fixture and thus you can have that in your test_get_user(...)
as
<pre><code>
def test_get_user(create_client, create_user):
print(create_user)
# do some tests here
</code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论