Mocking Models OutSide of Laravel
This all comes down to me using https://github.com/orchestral/testbench
With this in place I can work on an external library as if it is in Laravel.
The one place I ran into trouble on though was mocking model related methods like all
for example a model Foo
being called as \App\Foo::all()
was hard to mock cause all is stacic:
//\Illuminate\Database\Eloquent\Model::all
public static function all($columns = ['*'])
{
return (new static)->newQuery()->get(
is_array($columns) ? $columns : func_get_args()
);
}
But once I did this to my test:
public function testGetWithNoResults()
{
$mock = \Mockery::mock("alias:AlfredNutileInc\LaravelFeatureFlags\FeatureFlag");
$mock->shouldReceive('all')->once()->andReturn([]);
App::instance(FeatureFlag::class, $mock);
$fjs = new FeatureFlagsForJavascript();
$this->assertEmpty($fjs->get());
}
It was mocked!
comments powered by Disqus