Reputation: 5786
I want to test my views functionality for a Wagtail Page that uses the RoutablePageMixin. I've found that Wagtail includes some testing functionality, but I'm trying to check the content on different urls. Although the Wagtail test functions work, testing using self.client.get
doesn't work -- I get an 404 response. I'm trying the following test:
def test_subpage(self):
response = self.client.get(
self.page.full_url + self.page.reverse_subpage('subpage')
)
self.assertEqual(response.status_code, 200,
'Request the open positions page')
I assume the error lies in the way in which the page is created. I have used several ways, but cannot find one for which this works. The most intuitive way I found to create the page is the following:
def setUp(self):
self.login()
parent = Page.get_root_nodes()[0] # Home
self.assertCanCreate(parent, MyPage, {
'title': 'Title!',
'title_sv': 'Title2!',
'slug': 'test',
'published': datetime.datetime.now(),
})
self.page = MyPage.objects.get(slug='apply')
The subpages have been manually tested and do seem to work.
Upvotes: 3
Views: 918
Reputation: 25292
The simplest way to create a page within test code (or any other code...) is:
parent = Page.objects.get(url_path='/home/')
page = MyPage(title='Title!', slug='test', body='...')
parent.add_child(instance=page)
Note that Page.get_root_nodes()[0]
will not return your site homepage - the tree root is a non-editable placeholder, and site homepages are usually children of it. You can create additional children of the root, but unless you give them a corresponding Site record, they won't be accessible under any URL (which is probably why you're getting a 404 in your test).
Upvotes: 0