Spähen über das Internet, aber scheinen keine Antwort auf mein Problem zu finden. Ich habe in Laravel mit PHPUnit und Mothery in Test-Controller getaucht. Allerdings scheint ich meine eloquent-basierten Modelle nicht richtig zu verspotten. Ich habe es geschafft, meinen Auth :: user() auf die gleiche Weise zu verspotten, obwohl dies im folgenden Test nicht verwendet wird.Laravel 5 - Mit Mockery zu eloquenten Modell
Funktion in AddressController, die getestet werden soll:
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 Setup- und mock-Methode enthält
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 verlauf 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]', ['id' => $this->_address->id]);
$this->assertResponseStatus(404);
}
}
Der Fehler es Werfen hält, ist:
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.
Vielleicht hat jemand eine Idee?
Die Methode $ this-> call() wurde durch die Methode $ this-> action() ersetzt, während überprüft wurde, ob meine Controller-Methode überhaupt aufgerufen wurde und nach dem Ersetzen. Das Problem besteht jedoch weiterhin. –