1
我一直試圖在我的Laravel應用程序的控制器測試中使用Mockery一段時間,但似乎沒有任何工作。Laravel 5.0用戶雄辯模型嘲諷
UserController.php:
class UserController extends Controller
{
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Display a listing of the resource.
* GET /user
*
* @return Response
*/
public function index()
{
$users = $this->user->orderBy('created_at', 'desc')->paginate(6);
return view('users.index', compact('users'));
}
}
我的測試:
class UserControllerTest extends TestCase {
protected $user;
protected $builder;
protected $paginator;
public function setUp()
{
parent::setUp();
}
public function tearDown()
{
Mockery::close();
}
/**
* A basic functional test example.
*
* @return void
*/
public function testIndex()
{
$this->user = Mockery::mock('App\User');
$this->user
->shouldReceive('orderBy')
->once()
;
$this->app->instance('App\User', $this->user);
$response = $this->call('GET', 'users');
$this->assertEquals(200, $response->getStatusCode());
}
}
當我運行PHPUnit的我得到的輸出:
[Symfony的\分量\調試\異常\ FatalErrorException]
調用非對象的成員函數paginate()
當我添加 - > andReturn($這個 - >用戶):
$this->user
->shouldReceive('orderBy')
->once()
->andReturn($this->user)
;
我得到的PHPUnit的輸出:
Failed asserting that 500 matches expected 200.
Expected :200
Actual :500
當我打印響應它裏面以下錯誤:
Method Mockery_0_App_User::paginate() does not exist on this mock object
當我嘲笑paginate()函數時,它只是在視圖中'getIterator'函數失敗。
那麼我做錯了什麼? 我看過教程,閱讀文章和相關答案,但尚未找到解決方案。
帕吉納特有一些嘲弄的態度! –