I've looked through literally hundreds of pages, and I can't seem to find what makes my situation different than the working examples I've found on these pages.
I'm using Laravel 4.2, PHPUnit 4.3.1, and latest Mockery from Composer. I'm running a PHPUnit test on my Controller, AppController, and I'd like to intercept an Eloquent database save() by mocking the model. While there are no errors thrown when I create the Mock, the mock also isn't attaching to my model, so the rows are still being created.
What am I missing here? Thanks!
My error from Mockery: Mockery\Exception\InvalidCountException: Method save() from Mockery_0_EventRsvp should be called exactly 1 times but called 0 times.
// Located at /app/tests/app/AppControllerTest.php
class AppControllerTest extends TestCase {
public function setUp() {
parent::setUp();
Session::start();
Mail::pretend();
}
public function tearDown() {
parent::tearDown();
\Mockery::close();
}
public function testPostApp() {
$myvar = array();
$this->mock = \Mockery::mock('Eloquent','EventRsvp');
$this->app->instance('EventRsvp', $this->mock);
$this->mock
->shouldReceive('save')
->once()
->andReturn('true');
$response = $this->call('POST', '/3tDYSL0', $myvar);
}
}
// Located at /app/controllers/AppController
class AppController extends BaseController {
public function saveApp($shortUrl){
$rsvp = new EventRsvp;
$rsvp->fieldone = '124';
$rsvp->fieldtwo = '30233';
$rsvp->save();
$returnredirect = Redirect::to(Request::path(). '/complete');
return $returnredirect;
}
}
// Located at /app/models/EventRsvp.php
<?
class EventRsvp extends Eloquent {
protected $guarded = array('id');
use Illuminate\Database\Eloquent\SoftDeletingTrait;
protected $dates = ['deleted_at'];
public function relationshipone()
{
return $this->belongsTo('RelationshipOne','idone');
}
public function relationshiptwo()
{
return $this->belongsTo('RelationshipTwo','idtwo');
}
}
?>
new Class
in your controller. You can't refer to thatnew Class
by mocking, you need to inject mock to your controller. – Jarek Tkaczyk