简体   繁体   中英

“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?.

with silverstripe 4 unit test, im getting a error "Couldn't find object '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. 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.

Use this instead (note also changed them to protected visibility to match the parent class):

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.

Another suggestion: fixture files can also be relative file paths, so you can just use 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). Changing this won't affect your tests since what you have is fine, but it'll make it a little less verbose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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