我尝试在Django 1.7中测试我的列表视图:
class PostList(ListView):
model = MyBlog
template_name = 'posts.html'
queryset = 'some_query'
context_object_name = 'all_posts'
paginate_by = 25
class BlogTests(TransactionTestCase):
fixtures = ['blog.yaml']
def setUp(self):
self.client = Client()
def test_posts_page(self):
response = self.client.get(reverse_lazy('blog'))
self.assertEqual(response.status_code, 200, 'incorrect http code in posts page')
def test_posts_page_num_q(self):
self.assertNumQueries(3, PostList.as_view())
但我有个错误:
TypeError: view() takes at least 1 argument (0 given)
我不知道,怎么修。你知道吗
拜托,救命啊。你知道吗
谢谢。你知道吗
TestCase.assertNumQueries将预期的查询数作为第一个参数,将要测试的函数作为第二个参数,将要测试的函数的参数作为其余参数。你知道吗
在您的例子中,要测试的函数是一个需要一个参数的视图:
HttpRequest
。然后需要将HttpRequest
作为第三个参数传递给assertNumQueries
。您可以使用RequestFactory为测试目的创建HttpRequest
。你知道吗相关问题 更多 >
编程相关推荐