我在测试我的一个控制器时,无论我尝试什么方法,都会收到错误提示,说all()
函数不存在。
Static method Mockery_1_App_Models_User::all() does not exist on this mock object
我的测试方法:
/**
* Test index page
* @return void
*/
public function testIndexAsUser()
{
$this->beUser();
// The method calls the mock objects should receive
$this->user->shouldReceive('all')->once()->andReturn([]);
// Call index page
$response = $this->call('GET', 'users');
// Assertions
$this->assertResponseOk();
$this->assertViewHas('user');
$this->assertViewNameIs('users.show');
}
我的模拟方法:
/**
* Mock a class
* @param string $class
* @return Mockery
*/
public function mock($class)
{
$mock = Mockery::mock('Eloquent', $class);
app()->instance($class, $mock);
return $mock;
}
我的实际控制器方法:
/**
* Show all users
* @return Response
*/
public function getIndex()
{
$users = $this->user->all();
return view('users.index');
}
我在模拟对象中使用的Eloquent类是否有误?自从 Laravel 5 版本,模型不再引用 Eloquent 类,而是引用 Illuminate\Database\Eloquent\Model
,但我也尝试过这个。
Mockery::mock('Model', $class)
或者Mockery::mock('App\Model', $class)
了吗?(我知道你说过你试过使用完整的命名空间,但是你试过短版本的吗?) - Laurence