Laravel-Testing-Decoded
Laravel-Testing-Decoded copied to clipboard
Class 'Eloquent' not found when using Mockery::mock('Eloquent', 'Post') pg146
Hi Jeffrey,
On page 146 you talk about mocking Eloquent and Post classes. On line 8:
$this->mock = Mockery::mock('Eloquent', 'Post');
However running this test returns:
PHP Fatal error: Class 'Eloquent' not found
Incidentally this is also an issue in the tests your Laravel Generator generates through scaffold.
Has something changed in Laravel 4.1 to cause this issue? More importantly how do we work around it?
I've worked through the testing controllers section again and still getting the same results. Here's a repo of where I'm up to:
https://github.com/RyanHavoc/tdd-laravel
Running phpunit return the error:
PHP Fatal error: Class 'Eloquent' not found
If you mock eloquent before in the setup it works.
Maybe Mockery's api has changed?
Mockery::mock('Eloquent');
$this->mock = Mockery::mock('Post');
That is the code I used to get the tests passing.
Thank you Spencerdeinum so much for your post. I've been desperately trying to find out how to do this and your solution worked like a charm!
I'm a bit disappointed that Jeffrey doesn't update his book more often, I've already faced quite a few similar problems while working through his book.
V2 of the book is currently in development, and will be released this fall. :)
That's great news! I only started testing thanks to this book, I'm sure it'll attract even more people to TDD/BDD :)
Thanks @spencerdeinum finally got it working. I worked through the testing controllers chapter several times to ensure i was doing the right thing.
Use the setUp method, not use __construct
http://stackoverflow.com/questions/21339252/class-eloquent-not-found-when-mocking-in-laravel //Causes the Class 'Eloquent' not found error public function __construct() { $this->mock = Mockery::mock('Eloquent', 'Post'); }
//Setting the mocks in the setUp() method instead works public function setUp() { parent::setUp(); $this->mock = Mockery::mock('Eloquent', 'Post'); }
@mjfavaro
Thank you. That's the end of my search to solve this particular error.
I've come across this same part, but then with 5.1.
I use the full qualified name of Model, like:
$this->mock = Mockery::mock('Illuminate\Database\Eloquent\Model', 'App\Post');
Further to make it more verbose, I had to place this in the example methods:
$this->app->instance('App\Post', $this->mock);
It is working now, but I do get some more problems during following the examples in the book.
@aschwin hello,could you show me other codes? $this->mock ->shouldReceive('all') ->once() ->andReturn('foo');
$this->app->instance('App\Post',$this->mock);
$this->call('GET','posts');
$this->assertViewHas('posts');
This is my testing code.But when I run it,it show me the error "The response was not a view. Failed asserting that false is true. ". Thank you very much.
This is over 4 month but might help someone. I had the same issue as @kanxiaojie because my GET request was blocked by Auth middleware.
just call
$this->withoutMiddleware();
to disable Auth middleware on your test