【问题标题】:"Couldn't find object" error with Silverstripe 4 unit testing, how to fixed it?Silverstripe 4 单元测试出现“找不到对象”错误,如何解决?
【发布时间】:2019-02-04 01:02:14
【问题描述】:

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

使用 silverstripe 4 单元测试,我收到错误“找不到对象 'transaction1'”。 谁能建议这里发生了什么?谢谢。

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()
    );
}

 }

【问题讨论】:

标签: php silverstripe silverstripe-4 phpunit


【解决方案1】:

您完全重载了setUptearDown 方法。这些方法是 SapphireTest 处理您的装置的创建,以及设置配置清单等的地方。因为您已经重载了它,所以测试将无法运行。

改用这个(注意也将它们更改为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();
}

我没有看到你的夹具文件中有什么,所以我认为它很好,问题只是它没有加载。

另一个建议:夹具文件也可以是相对文件路径,因此如果文件与测试类存在于同一目录中,则可以使用 protected static $fixture_file = 'CustomerCreditTransactionTest.yml';(如果您的夹具位于 mysite/code 中,那么我假设它不存在) t,你可以忽略这个建议)。更改此设置不会影响您的测试,因为您所拥有的一切都很好,但它会使其不那么冗长。

【讨论】:

  • 我进行了更改并运行了测试,结果是一个几乎没有改进的错误,....错误是SilverStripe\ORM\Connect\DatabaseException:Couldn't run query:INSERT INTO "CustomerCreditTransaction_Client" ("Created") VALUES(NOW())42S02-1146: Table 'ss_tmpdb_1535708859_4128594.CustomerCreditTransaction_Client' ``doesn't exist
  • @kosalamanojeewa 确保将 '' flush=1 添加到 phpunit 命令的末尾,否则您需要向我们展示您的夹具文件 =)
  • 此处是夹具文件,namespace\CustomerCreditTransaction: 987654335 Amount: 100 987654337 Customer: => namespace\Customer.Jone 987654339 Amount: 200 987654341 Customer: => namespace\Customer.Joe 987654343 customer1: 987654345 @ Email: John@gmail.comcustomer2:FirstName: JoeEmail: Joe@gmail.com
猜你喜欢
  • 1970-01-01
  • 2020-07-25
  • 2013-11-15
  • 1970-01-01
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 2019-08-17
相关资源
最近更新 更多