简体   繁体   English

Silverstripe 4单元测试中出现“找不到对象”错误,如何解决?

[英]“Couldn't find object” error with Silverstripe 4 unit testing, how to fixed it?

"Couldn't find object" error with Silverstripe 4 unit testing, how to fixed it?. Silverstripe 4单元测试中出现“找不到对象”错误,如何解决?

with silverstripe 4 unit test, im getting a error "Couldn't find object 'transaction1' ". 在silverstripe 4单元测试中,我收到一个错误“找不到对象'transaction1'”。 can anyone suggest what's happening here?. 有人可以建议这里发生了什么吗? Thanks. 谢谢。

class CustomerCreditTransactionTest extends SapphireTest 
{



protected static $fixture_file = BASE_PATH.'/mysite/code/CustomerCreditTransactionTest.yml';


/**
 * @var string
 */
protected $readingmode = null;
/**
 * Default reading mode
 *
 * @var string
 */
protected $defaultMode = null;


public function setUp()
{
    $this->readingmode = 'Original';
    $this->defaultMode = 'Original';
}
public function tearDown()
{
    MirroredData::SetCurrentReadingStage($this->readingmode);
    MirroredData::SetDefaultWritingStage($this->defaultMode);
}


public function testCustomerName()
{
    $obj = $this->objFromFixture(CustomerCreditTransaction::class, 'transaction1');

    $this->assertEquals(
        'John@gmail.com',
        $obj->CustomerName(),
        'customer name is : '.$obj->CustomerName()
    );
}

 }

You overload the setUp and tearDown methods entirely. 您将完全重载setUptearDown方法。 These methods are where SapphireTest handled the creation of your fixtures, as well as setting up config manifests, etc. Because you've overloaded it the tests won't run. 这些方法是SapphireTest处理夹具创建以及设置配置清单等的地方。因为您已经超载了它,所以测试将无法运行。

Use this instead (note also changed them to protected visibility to match the parent class): 请改用此方法(请注意,也将它们更改为protected可见性以匹配父类):

protected function setUp()
{
    parent::setUp();

    $this->readingmode = 'Original';
    $this->defaultMode = 'Original';
}

protected function tearDown()
{
    MirroredData::SetCurrentReadingStage($this->readingmode);
    MirroredData::SetDefaultWritingStage($this->defaultMode);

    parent::tearDown();
}

I don't see what's in your fixture file, so I'll assume that it's fine and that the issue is only that it's not loaded. 我看不到您的Fixture文件中有什么内容,因此我认为这没问题,而问题仅在于未加载。

Another suggestion: fixture files can also be relative file paths, so you can just use protected static $fixture_file = 'CustomerCreditTransactionTest.yml'; 另一个建议:灯具文件也可以是相对文件路径,因此您可以使用protected static $fixture_file = 'CustomerCreditTransactionTest.yml'; if the file exists in the same directory as your test class (if your fixture lives in mysite/code then I assume it doesn't and you can ignore this suggestion). 如果文件与测试类位于同一目录中(如果您的灯具位于mysite/code那么我认为它不存在,那么您可以忽略此建议)。 Changing this won't affect your tests since what you have is fine, but it'll make it a little less verbose. 更改此设置不会影响您的测试,因为您所拥有的还可以,但是会使它的详细程度降低一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM