def test_get_object(self, user: User, rf: RequestFactory): view = UserUpdateView() request = rf.get("/fake-url/") request.user = user view.request = request assert view.get_object() == user
class TestUserUpdateView(BaseUserTestCase): def setUp(self): super().setUp() self.view = UserUpdateView() request = self.factory.get('/fake-url') request.user = self.user self.view.request = request def test_get_success_url(self): self.assertEqual(self.view.get_success_url(), '/users/testuser/') def test_get_object(self): self.assertEqual(self.view.get_object(), self.user)
class TestUserUpdateView(BaseUserTestCase): def setUp(self): super().setUp() self.view = UserUpdateView() request = RequestFactory().get("/fake-url/") request.user = self.user self.view.request = request def test_get_success_url(self): assert self.view.get_success_url() == f"/users/{self.user.username}/" def test_get_object(self): assert self.view.get_object() == self.user
class TestUserUpdateView(BaseUserTestCase): def setUp(self) -> None: super().setUp() self.view = UserUpdateView() # 创建请求,不通过中间件 request = self.factory.get("fake_url") request.user = self.user self.view.request = request def test_get_success_url(self): self.assertEqual(self.view.get_success_url(), "/users/testuser/") def test_get_object(self): self.assertEqual(self.view.get_object(), self.user)
class TestUserUpdateView(BaseUserTestCase): def setUp(self): super().setUp() # 继承父类的方法 self.view = UserUpdateView() request = self.factory.get("/fake-url") request.user = self.user self.view.request = request # 把request请求发送给视图 def test_get_success_url(self): self.assertEqual(self.view.get_success_url(), "/users/testuser/") def test_get_object(self): self.assertEqual(self.view.get_object(), self.user)
class TestUpdateView(BaseUserTestCase): def setUp(self): # 继承父类方法 super().setUp() # 添加测试视图 self.view = UserUpdateView() # 自定义request请求 url任意,不是给url发送请求(url不存在不响应都行) 而是创建请求 request = self.factory.get('/fake-url') # 将当前用户 加入 改次请求中 request.user = self.user # 将该次请求发送给视图 self.view.request = request def test_get_success_url(self): self.assertEqual(self.view.get_success_url(),'/users/testuser/') def test_get_object(self): self.assertEqual(self.view.get_object(),self.user)