Unit testing 根据POST请求进行的SilverStripe Sapphire功能测试始终返回404状态
我正在做一个SilverStripe项目。我正试图根据本文档为我的应用程序编写功能测试。我正在测试一个POST请求。但它不起作用。你可以在下面看到我的代码 我有一个名为CustomFormPageController的控制器类,代码如下Unit testing 根据POST请求进行的SilverStripe Sapphire功能测试始终返回404状态,unit-testing,silverstripe,functional-testing,silverstripe-4,Unit Testing,Silverstripe,Functional Testing,Silverstripe 4,我正在做一个SilverStripe项目。我正试图根据本文档为我的应用程序编写功能测试。我正在测试一个POST请求。但它不起作用。你可以在下面看到我的代码 我有一个名为CustomFormPageController的控制器类,代码如下 class CustomFormPageController extends PageController { private static $allowed_actions = [ 'testPostRequest', ];
class CustomFormPageController extends PageController
{
private static $allowed_actions = [
'testPostRequest',
];
private static $url_handlers = [
'testPostRequest' => 'testPostRequest',
];
public function testPostRequest(HTTPRequest $request)
{
if (! $request->isPOST()) {
return "Bad request";
}
return "Request successfully processed";
}
}
我还为该控制器提供了一个名为CustomFormPage的页面类。下面是该类的实现
class CustomFormPage extends Page
{
}
我试图测试的是,我试图测试testPostRequest方法是否返回正确的值
下面是我的测试课
class CustomFormPageTest extends FunctionalTest
{
protected static $fixture_file = 'fixtures.yml';
public function testTestingPost()
{
$formPage = $this->objFromFixture(CustomFormPage::class, 'form_page');
$response = $this->post($formPage->URLSegment . '/testPostRequest', [
'name' => 'testing'
]);
var_dump($response);
}
}
以下是我的fixtures.yml文件
SilverStripe\CMS\Model\SiteTree:
sitetree_page_one:
ID: 1
ClassName: CustomFormPage
Title: Page Title 1
URLSegment: custom-form-page
CustomFormPage:
form_page:
ID: 1
Title: Page Title 1
URLSegment: custom-form-page
当我运行测试时,它总是返回404NotFound状态,即使页面是在数据库中创建的。我的代码中缺少了什么以及如何修复它?我刚刚找到了解决方案。我们需要在测试中发布页面,如下所示
$formPage->publish("Stage", "Live");
您还可以使用
protectedstatic$use\u draft\u site=true
使用draft而不是live