我有一個模型,從Toddish \繼承驗證Laravel包(https://github.com/Toddish/Verify-L4/blob/master/src/Toddish/Verify/Models/User.php)Laravel 4繼承屬性是空的
所有我想要做的就是添加一些屬性:
use Toddish\Verify\Models\User as VerifyUser;
class User extends VerifyUser
{
public function __construct (array $attributes = array()) {
parent::__construct($attributes);
$this->fillable = array_merge ($this->fillable, array(
'salutation', 'title', 'firstname', 'lastname', 'phonenumber', 'mobilenumber'
));
}
}
當我運行測試:
class UserTest extends TestCase {
public function testUserCreation() {
$user = User::create(
[
'username' => 'testusername',
'email' => '[email protected]',
'password' => 'testpassword',
'salutation' => 'MrTest',
'title' => 'MScTest',
'firstname' => 'Testfirstname',
'lastname' => 'Testlastname',
'phonenumber' => 'testPhoneNumber',
'mobilenumber' => 'testMobileNumber',
]
);
$this->assertEquals($user->salutation, 'MrTest');
$this->assertEquals($user->title, 'MScTest');
$this->assertEquals($user->firstname, 'Testfirstname');
$this->assertEquals($user->lastname, 'Testlastname');
$this->assertEquals($user->phonenumber, 'testPhoneNumber');
$this->assertEquals($user->mobilenumber, 'testMobileNumber');
}
}
我得到如下:
1) UserTest::testUserCreation
Failed asserting that 'MrTest' matches expected null.
所有的斷言都返回null。但我檢查了數據庫列存在。那麼爲什麼這個屬性爲空?
編輯:
如果我換斷言參數:
$this->assertEquals('MrTest', $this->salutation);
我得到這個:
ErrorException: Undefined property: UserTest::$salutation
作爲一個側面說明一些解釋的部分,你的斷言參數是向下,則預期結果應該是第一位的。這會讓你的錯誤信息更有意義;) – duellsy
很酷,謝謝!現在錯誤是「ErrorException:Undefined屬性:UserTest :: $ salutation」 –
你的斷言是什麼?它應該是'$ this-> assertEquals('MrTest',$ user-> salutation);' – duellsy