我开始在 Laravel 4 中进行单元测试,并且我一直在测试我添加到标准用户模型中的模型中的自定义方法。
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends BaseModel implements UserInterface, RemindableInterface {
/**
* Logs to the database and streams an update
* Uses logIt and streamIt on Base model
* @param String $action The action being performed
* @return void
*/
private function logAndStream($action)
{
$this->logIt('info', $action.'d user '.$this->username);
$this->streamIt($action.'d user '.$this->username);
}
此类扩展了 BaseModel,后者又扩展了 Eloquent,并定义了 logIt 和 StreamIt 方法,如下所示:
class BaseModel extends Eloquent {
/**
* Log an action to log file
* @return void
*/
protected function logIt($level, $msg) {
...
}
/**
* Log an action to activity stream
* @return void
*/
protected function streamIt($msg, $client = null, $project = null) {
...
}
当我手动测试时,所有这些代码都可以正常工作。但现在我想创建一个单元测试来自动化它。
class UserTest extends TestCase {
public function testLogAndStream()
{
$base = Mockery::mock('BaseModel')->shouldAllowMockingProtectedMethods();
$base->shouldReceive('logIt')
->with('info', 'Created user Tester')
->once();
$user = new User;
$user->username = 'Tester';
$user->logAndStream('Create');
}
当我尝试运行它时,我抱怨找不到 logAndStream 失败。
1) UserTest::testLogAndStream
BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::logAndStream()
我错过了什么?