Code: Select all
public function testInstantiateDatastoreLoadsStorageModel(){
$this->_storageModel->expectOnce('load');
$dataStore = new DataStore($this->_storageModel);
}
Moderator: General Moderators
Code: Select all
public function testInstantiateDatastoreLoadsStorageModel(){
$this->_storageModel->expectOnce('load');
$dataStore = new DataStore($this->_storageModel);
}
Code: Select all
public function testInstantiateDatastoreLoadsStorageModel(){
$storageModel = new MockStorageModel();
$storageModel->expectOnce('load');
$dataStore = new DataStore($storageModel);
}
Code: Select all
public function __construct(StorageModel $storageModel) {
$this->_storageModel = $storageModel;
$this->_storageModel->load();
}
Code: Select all
class DataStoreTest ...
public function setUp() {
$this->_storageModel = $this->_createStorageModel();
$this->_dataStore = $this->_createDataStore($this->_storageModel);
}
....
public function testInstantiateDatastoreLoadsStorageModel() {
$storageModel = $this->_createStorageModel();
$storageModel->expectOnce('load');
$this->_createDataStore($storageModel);
}
....
private function _createStorageModel() {
return new MockStorageModel();
}
private function _createDataStore(StorageModel $storageModel) {
return new DataStore($storageModel);
}
}Code: Select all
class DataStoreTest ...
...
public function testDataStoreLoadsValuesFromStorageModel() {
$storageModel = $this->_createStorageModel();
$storageModel->setReturnValue('load', array('zip' => 'button'));
$dataStore = $this->_createDataStore($storageModel);
$this->assertEqual('button', $dataStore->get('zip'),
'%s: Values from StorageModel should be loaded into DataStore'
);
}Code: Select all
AllTests.php
1) Equal expectation fails at character 0 with [button] and []: Values from StorageModel should be loaded into DataStore at [/Users/d11wtq/data_store/tests/unit/DataStoreTest.php line 60]
in testDataStoreLoadsValuesFromStorageModel
in DataStoreTest
in /Users/d11wtq/data_store/tests/unit/DataStoreTest.php
in All DataStore tests
FAILURES!!!
Test cases run: 1/1, Passes: 7, Failures: 1, Exceptions: 0That's right. Most of the time the SUT can be tested in it's already instantiated state. But when you're testing something which happens during instantiation then factory methods are a good trade-off. Again, there's not seriously wrong with just skipping using setUp() and/or using factory methods.... but all those calls to "new Xyz()" make refactoring harder.matthijs wrote:So basically by adding those private functions and calling them in testInstantiateDatastoreLoadsStorageModel, you re-instantiate the DataStore and StorageModel classes, so that the constructor can be tested?
Code: Select all
public function __construct(StorageModel $storageModel) {
$this->_storageModel = $storageModel;
$this->_storageModel->load();
}
System Under Testmatthijs wrote:Two quick questions: what is SUT?
I did, I've attached my updated files (including the new failing test we have to write code forAnd what did you do in the previous test to make it pass? This?Code: Select all
public function __construct(StorageModel $storageModel) { $this->_storageModel = $storageModel; $this->_storageModel->load(); }
Code: Select all
public function testRemoveValueRemovesValue(){
$this->_dataStore->set('foo', 'bar');
$this->assertTrue($this->_dataStore->has('foo'));
$this->_dataStore->remove('foo');
$this->assertFalse($this->_dataStore->has('foo'));
}
Code: Select all
public function __construct(StorageModel $storageModel) {
$this->_storageModel = $storageModel;
if(is_array($this->_storageModel->load()))
$this->_store = $this->_storageModel->load();
}
I agree it's ugly... we can come back to it later. But maybe this is an improvement?arjan.top wrote:ugly ...if there is no if testDataStoreKnowsWhenItDoesntHaveAValue failsCode: Select all
public function __construct(StorageModel $storageModel) { $this->_storageModel = $storageModel; if(is_array($this->_storageModel->load())) $this->_store = $this->_storageModel->load(); }
Code: Select all
public function __construct(StorageModel $storageModel) {
$this->_store = (array) $storageModel->load();
$this->_storageModel = $storageModel;
}Smurf, I was trying to lessen my caffeine ingestion (they say it's not healthy) but obviously that was not a good decisionarjan.top wrote:no it's not
You assign $this->_storageModel->load() to $this->_store, but $this->_storageModel->load() in setUp has no return value so $this->_store is no longer an array