SilverStripe蓝宝石单元测试抛出错误,当使用objFromFixture方法从灯具中检索对象时,“找不到对象”

问题描述 投票:0回答:1

我正在从事SilverStripe项目。我现在正在尝试为我的应用程序编写单元测试和功能测试。在测试中,我使用Fixture文件将页面植入数据库。当我尝试为SilverStripe项目内置的Page类的固定装置播种时,就很好了。以下是我的代码。

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: EvenPage) 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

我的考试有什么问题,我该如何解决?

unit-testing phpunit silverstripe silverstripe-4
1个回答
0
投票

读取错误消息:

Fatal error: Couldn't find object 'new_page' (class: Eventage)

[似乎在测试代码中有一个错字,其中Eventage应该为EventPage

© www.soinside.com 2019 - 2024. All rights reserved.