Unit testing SilverStripe sapphire单元测试抛出错误;不能';“找不到对象”;使用objFromFixture方法从夹具检索对象时
我正在做一个SilverStripe项目。我现在正试图为我的应用程序编写单元测试和功能测试。在我的测试中,我使用fixture文件在数据库中对页面进行种子设定。当我尝试为SilverStripe项目内置的Page类植入fixture时,一切正常。下面是我的代码Unit testing SilverStripe sapphire单元测试抛出错误;不能';“找不到对象”;使用objFromFixture方法从夹具检索对象时,unit-testing,phpunit,silverstripe,silverstripe-4,Unit Testing,Phpunit,Silverstripe,Silverstripe 4,我正在做一个SilverStripe项目。我现在正试图为我的应用程序编写单元测试和功能测试。在我的测试中,我使用fixture文件在数据库中对页面进行种子设定。当我尝试为SilverStripe项目内置的Page类植入fixture时,一切正常。下面是我的代码 class PageTest extends \SilverStripe\Dev\FunctionalTest { protected static $fixture_file = 'fixtures.yml'; pub
class PageTest extends \SilverStripe\Dev\FunctionalTest
{
protected static $fixture_file = 'fixtures.yml';
public function testMyMethod()
{
$expectedURLs = [
'new_page' => 'testing1',
'old_page' => 'testing2',
];
foreach ($expectedURLs as $fixture => $urlSegment) {
$obj = $this->objFromFixture('Page', $fixture);
$this->assertEquals($urlSegment, $obj->URLSegment);
}
}
}
这是我的固定档案
Page:
new_page:
Title: New Page 1
Heading: Wellington
PublishedDate: 2015-12-12 00:00:01
ShowInFooterUsefulLinks: true
PageTheme: Default
URLSegment: testing1
old_page:
Title: Old Page 1
Heading: Wellington 1
PublishedDate: 2015-12-12 00:00:01
ShowInFooterUsefulLinks: true
PageTheme: Default
URLSegment: testing2
上述测试按预期进行
在我的测试中,我想改用EventPage类。我有一个名为EventPage的类,它是从Page类扩展而来的。该EventPage类表有一个名为EventDate的额外字段。因此,我将我的夹具文件修改为下面的文件
EventPage:
new_page:
Title: New Page 1
Heading: Wellington
PublishedDate: 2015-12-12 00:00:01
ShowInFooterUsefulLinks: true
PageTheme: Default
URLSegment: testing1
EventDate: 2015-12-12 00:00:01
old_page:
Title: Old Page 1
Heading: Wellington 1
PublishedDate: 2015-12-12 00:00:01
ShowInFooterUsefulLinks: true
PageTheme: Default
URLSegment: testing2
EventDate: 2015-12-12 00:00:01
这次我改用EventPage类
我还将我的测试类修改为
class PageTest extends \SilverStripe\Dev\FunctionalTest
{
protected static $fixture_file = 'fixtures.yml';
public function testMyMethod()
{
$expectedURLs = [
'new_page' => 'testing1',
'old_page' => 'testing2',
];
foreach ($expectedURLs as $fixture => $urlSegment) {
$obj = $this->objFromFixture('EventPage', $fixture);
$this->assertEquals($urlSegment, $obj->URLSegment);
}
}
}
当我运行测试时,我得到了以下错误
Fatal error: Couldn't find object 'new_page' (class: EventPage) in /var/www/vendor/silverstripe/framework/src/Dev/SapphireTest.php on line 505
Call Stack:
0.0013 349392 1. {main}() /var/www/vendor/phpunit/phpunit/phpunit:0
0.1998 520368 2. PHPUnit_TextUI_Command::main() /var/www/vendor/phpunit/phpunit/phpunit:52
0.1999 520480 3. PHPUnit_TextUI_Command->run() /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php:116
0.4674 1128776 4. PHPUnit_TextUI_TestRunner->doRun() /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php:186
0.5089 1210224 5. PHPUnit_Framework_TestSuite->run() /var/www/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:517
0.5124 1210752 6. PHPUnit_Framework_TestSuite->run() /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php:733
4.7228 16476656 7. PHPUnit_Framework_TestSuite->run() /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php:733
4.7573 16484232 8. PageTest->run() /var/www/vendor/phpunit/phpunit/src/Framework/TestSuite.php:733
4.7574 16484232 9. PHPUnit_Framework_TestResult->run() /var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php:868
4.7577 16484608 10. PageTest->runBare() /var/www/vendor/phpunit/phpunit/src/Framework/TestResult.php:686
22.8349 39277864 11. PageTest->runTest() /var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php:913
22.8350 39278160 12. ReflectionMethod->invokeArgs() /var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php:1062
22.8350 39278168 13. PageTest->testMyMethod() /var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php:1062
22.8350 39278168 14. PageTest->objFromFixture() /var/www/app/tests/PageTest.php:17
22.8351 39278488 15. user_error() /var/www/vendor/silverstripe/framework/src/Dev/SapphireTest.php:505
我的测试出了什么问题?如何修复它?您刷新了缓存吗?您刷新了缓存吗?