-
-
Save JeffreyWay/4968363 to your computer and use it in GitHub Desktop.
<?php | |
class BaseModel extends Eloquent { | |
public static function shouldReceive() | |
{ | |
$repo = get_called_class() . 'RepositoryInterface'; | |
$mock = Mockery::mock($repo); | |
App::instance($repo, $mock); | |
return call_user_func_array(array($mock, 'shouldReceive'), func_get_args()); | |
} | |
} |
<?php | |
class PostsTest extends TestCase { | |
public function testAllPosts() | |
{ | |
// This will mock the PostRepository interface, update the instance | |
// that will be injected into the controller (use DI), and stub the getAll method. | |
// This way, the DB will never be hit. | |
Post::shouldReceive('getAll')->andReturn('foo'); | |
// Call the route. | |
$response = $this->call('GET', 'posts'); | |
// Make sure that the request was successful. | |
$this->assertTrue($response->isOk()); | |
// also make sure that $posts is bound to the view | |
$this->assertEquals('foo', $response->getOriginalContent()->posts); | |
} | |
} |
@danharper Just saw your question. My Repository tests also use sqlite in memory, rather than mocking the Foo. I get the same bug as you do when I try to mock Foo. I'm not certain, but I think that the problem is that all() is a static method.
@danharper in the framework tests, they extend the Eloquent class with a stub, to replace the newQuery method. IT is a bit convoluted, but it might work:
https://github.com/laravel/framework/blob/master/tests/Database/DatabaseEloquentModelTest.php
@karptonite Ah thanks, I'll give it a try! Only just noticed your reply, guess GitHub doesn't create notifications on mentions in Gists.
I had trouble using this snippet. I've found the problem to be namespaces. I thought I would share my modified code.
My repositories are in the Repositories namespace and models are in the Models namespace.
public static function shouldReceive($args)
{
$calledClassParts = explode('\\', get_called_class());
$calledClass = end($calledClassParts);
$repo = 'Repositories\\' . $calledClass . 'RepositoryInterface';
$mock = Mockery::mock($repo);
App::instance($repo, $mock);
return call_user_func_array([$mock, 'shouldReceive'], func_get_args());
}
@karptonite How are you testing your
EloquentFooRepository
and mocking the injectedFoo
?eg.:
Throws:
I'm sure I'm missing something with the mock...
Calling it in the controller works and returns the correct DB data:
Edit: I'm now just using a sqlite db in memory instead of mocking it.