3

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

 }
kosala manojeewa
  • 329
  • 3
  • 11

1 Answers1

0

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.

scrowler
  • 24,273
  • 9
  • 60
  • 92
  • I made the changes and run the test, result was a error with little improvement, ....error is `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` – kosala manojeewa Aug 31 '18 at 09:54
  • @kosalamanojeewa ensure you add `'' flush=1` to the end of your phpunit command, otherwise you'll need to show us your fixture file =) – scrowler Sep 02 '18 at 12:10
  • here is the fixture file, `namespace\CustomerCreditTransaction:` `transaction1:` `Amount: 100` `Comment: testing comment 1` `Customer: => namespace\Customer.Jone` `transaction2:` `Amount: 200` `Comment: testing comment 2` `Customer: => namespace\Customer.Joe` `namespace\Customer:` `customer1:` `FirstName: John` `Email: John@gmail.com` `customer2:` `FirstName: Joe` `Email: Joe@gmail.com` – kosala manojeewa Sep 03 '18 at 03:02