I'm using a library to send send requests to Indeed jobs https://github.com/jobapis/jobs-indeed.
I have setup a provider so I can easily mock the requests and also so I don't have to setup my credentials every time I use it.
This library has 2 classes. A Query and Provider class. The Provider class is responsible for making the http request.
I can mock the Query class but I can't mock the Provider class.
Provider:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use JobApis\Jobs\Client\Queries\IndeedQuery;
use JobApis\Jobs\Client\Providers\IndeedProvider;
class JobSearchServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
// Register Indeeds API
$this->app->bind(IndeedQuery::class, function() {
$request = app('request');
// Build the required fields for indeeds api
$indeed = new IndeedQuery([
'publisher' => config('services.indeed.publisher'),
'format' => 'json',
'v' => '2',
]);
// These fields come off the request
$indeed->set('userip', $request->ip());
$indeed->set('useragent', $request->header('User-Agent'));
return $indeed;
});
$this->app->bind(IndeedProvider::class, function() {
// Use an empty query object so that we can initialise the provider and add the query in the controller.
$queryInstance = app('JobApis\Jobs\Client\Queries\IndeedQuery');
return new IndeedProvider($queryInstance);
});
}
}
Controller:
public function searchIndeed(Request $request, IndeedQuery $query, IndeedProvider $client)
{
dump($query); // Returns a mockery object
dd($client); // Returns original object
}
Test:
public function testSearchIndeed()
{
$user = factory(User::class)->create();
$this->mock(IndeedQuery::class);
$this->mock(IndeedProvider::class);
$this->actingAs($user)
->get('indeed')
->assertStatus(200);
}
Why is the IndeedQuery being mocked but not the IndeedProvider?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire