SilverStripe 4 : FunctionalTest "get" 方法 returns 404 状态,尽管页面在那里。

SilverStripe 4 : FunctionalTest "get" method returns 404 status although the page is there.

我正在尝试使用此测试来测试控制器 class,

<?php

use SilverStripe\Dev\FunctionalTest;

class SitePageControllerTest extends FunctionalTest
{
    protected static $fixture_file = 'site/tests/fixturesSitePage.yml';

    public function testViewSitePage()
    {
        $obj = $this->objFromFixture('SitePage', 'page1');
        $page = $this->get('page-one/');
        $this->assertEquals(200, $page->getStatusCode());
    }
}

和夹具。

SitePage:
  page1:
    Title: Page One
    CanViewType: true

但是“$this->get('page-one/');” returns 一个 404 页面。

页面是版本化的,并且这个页面没有在您要求的时候发布,所以功能测试模拟了一个前端网络请求,默认情况下从实时(发布)阶段提供服务。

您可以通过将 ?stage=Stage 附加到您的请求 URL 或在您的功能测试中使用 protected static $use_draft_site = true 来使用草稿站点(这在 4.2 中已弃用)。

请注意,FunctionalTest 不会让用户登录,因此您可能还需要使用某种级别的权限登录,即 $this->logInWithPermission('ADMIN')

另一种选择是使用类似的方式发布它:$obj->publishRecursive();get()

之前