【发布时间】:2016-01-06 18:23:44
【问题描述】:
我已经在 Laravel 4 和 Laravel 5 中构建了 laravel 应用程序,但这次我决定先编写我所有的测试,以前从未为琐碎的应用程序编写过测试。
这是我的 Account 类 - 用于说明
class Account extends Model
{
protected $customer_id;
protected $bookmaker_id;
protected $balance;
protected $profit;
public function __construct($customer_id, $bookmaker_id, $balance, $profit) {
$this->customer_id = $customer_id;
$this->bookmaker_id = $bookmaker_id;
$this->balance = $balance;
$this->profit = $profit;
}
}
所以我所有的单元测试都运行良好:
我的路线已正确设置到我要显示的页面
Route::get('/accounts', 'AccountController@index');
但这就是问题所在。实际上尝试运行一个页面来获取帐户列表是很麻烦的。我知道控制器类还有很多事情要做,但这就是我所拥有的。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Account;
class AccountController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$accounts = Account::all();
return view('account.index', compact('accounts'));
}
}
然后我得到这个错误 -
ErrorException in Account.php line 14:
Missing argument 1 for App\Account::__construct(), called in /Applications/MAMP/htdocs/mb-app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 665 and defined
谁能告诉我应该如何设置我的控制器?在我为单元测试添加 __construct() 之前,一切正常。
谢谢。
【问题讨论】:
标签: php unit-testing laravel-5.2 construct