Laravel-Testing-Decoded icon indicating copy to clipboard operation
Laravel-Testing-Decoded copied to clipboard

Class 'Eloquent' not found when using Mockery::mock('Eloquent', 'Post') pg146

Open RyanHavoc opened this issue 11 years ago • 11 comments

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?

RyanHavoc avatar Jan 24 '14 16:01 RyanHavoc

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

RyanHavoc avatar Jan 25 '14 11:01 RyanHavoc

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.

spencerdeinum avatar May 05 '14 05:05 spencerdeinum

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.

lithin avatar Jun 06 '14 08:06 lithin

V2 of the book is currently in development, and will be released this fall. :)

laracasts avatar Jun 06 '14 19:06 laracasts

That's great news! I only started testing thanks to this book, I'm sure it'll attract even more people to TDD/BDD :)

lithin avatar Jun 06 '14 19:06 lithin

Thanks @spencerdeinum finally got it working. I worked through the testing controllers chapter several times to ensure i was doing the right thing.

nathvarun avatar Jul 23 '14 09:07 nathvarun

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 avatar Oct 23 '14 19:10 mjfavaro

@mjfavaro

Thank you. That's the end of my search to solve this particular error.

markdwhite avatar Nov 04 '14 07:11 markdwhite

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 avatar Jul 15 '15 15:07 aschwin

@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.

kanxiaojie avatar Apr 21 '16 09:04 kanxiaojie

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

karingisi avatar Aug 27 '16 19:08 karingisi