我正在从事SilverStripe项目。我正在尝试按照https://docs.silverstripe.org/en/4/developer_guides/testing/functional_testing/文档为我的应用程序编写功能测试。我正在测试POST请求。但这是行不通的。您可以在下面看到我的代码。
我有一个名为CustomFormPageController的控制器类,带有以下代码。
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:
cision_subscription_form_page_one:
ID: 1
Title: Page Title 1
URLSegment: custom-form-page
我运行测试时,即使页面是在数据库中创建的,它也始终返回404 not found状态。我的代码中缺少什么,我该如何解决?
CustomFormPage
的句柄为cision_subscription_form_page_one
。 获取CustomFormPage
的代码应为:
$formPage = $this->objFromFixture(CustomFormPage::class, 'cision_subscription_form_page_one');