一直在尋找所有在互聯網上,但似乎還沒有找到解決我的問題。我一直在使用PHPUnit和Mockery進入Laravel測試控制器。但是,我似乎並沒有讓我基於雄辯的模型正確地被嘲笑。我確實設法以相同的方式嘲弄我的Auth :: user(),雖然這在下面的測試中沒有使用。在AddressControllerLaravel 5 - 用嘲弄嘲弄雄辯模型
功能需要被測試:
public function edit($id)
{
$user = Auth::user();
$company = Company::where('kvk', $user->kvk)->first();
$address = Address::whereId($id)->first();
if(is_null($address)) {
return abort(404);
}
return view('pages.address.update')
->with(compact('address'));
}
含有延伸ControllerTest
class AddressControllerTest extends ControllerTest
{
/**
* @var \App\Models\Address
*/
private $_address;
/**
* @var \App\Http\Controllers\AddressController
*/
protected $_controller;
public function setUp(){
parent::setUp();
$this->_controller = new AddressController();
$this->_address = factory(Address::class)->make();
}
public function testEdit404(){
$companyMock = $this->mock(Company::class);
$companyMock
->shouldReceive('where')
->with('kvk', Mockery::any())
->once();
->andReturn(factory(Company::class)->make([
'address_id' => $this->_address->id
]));
$addressMock = $this->mock(Address::class);
$addressMock
->shouldReceive('whereId')
->with($this->_address->id)
->once();
->andReturn(null);
//First try to go to route with non existing address
$this->action('GET', '[email protected]edit', ['id' => $this->_address->id]);
$this->assertResponseStatus(404);
}
}
它保持投擲是該錯誤安裝程序和模擬方法
abstract class ControllerTest extends TestCase
{
/**
* @var \App\Http\Controllers\Controller
*/
protected $_controller;
public function setUp(){
parent::setUp();
$this->createApplication();
}
public function tearDown()
{
parent::tearDown();
Mockery::close();
}
protected function mock($class)
{
$mock = Mockery::mock($class);
$this->app->instance($class, $mock);
return $mock;
}
}
AddressControllerTest ControllerTest:
1) AddressControllerTest::testEdit404
Mockery\Exception\InvalidCountException: Method where("kvk", object(Mockery\Matcher\Any)) from Mockery_1_Genta_Models_Company should be called exactly 1 times but called 0 times.
也許任何人有一個想法?
,同時檢查,如果我的控制器的方法得到了所有調用,它置換後的確與$這個 - >行動()方法取代了$這個 - >()方法調用。但問題依然存在。 –