Laravel 为什么服务提供商不会被嘲笑?
我正在使用一个库向乔布斯发送请求 我已经设置了一个提供者,这样我就可以轻松地模拟请求,也就不必每次使用它时都设置凭据 这个图书馆有两个班。查询和提供程序类。提供者类负责发出http请求 我可以模拟查询类,但不能模拟提供者类 提供者:Laravel 为什么服务提供商不会被嘲笑?,laravel,laravel-5,phpunit,Laravel,Laravel 5,Phpunit,我正在使用一个库向乔布斯发送请求 我已经设置了一个提供者,这样我就可以轻松地模拟请求,也就不必每次使用它时都设置凭据 这个图书馆有两个班。查询和提供程序类。提供者类负责发出http请求 我可以模拟查询类,但不能模拟提供者类 提供者: namespace App\Providers; use Illuminate\Support\ServiceProvider; use JobApis\Jobs\Client\Queries\IndeedQuery; use JobApis\Jobs\Clien
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() {
// Build the required fields for indeeds api
$indeed = new IndeedQuery([
'publisher' => config('services.indeed.publisher'),
'format' => 'json',
'v' => '2',
]);
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);
});
}
}
控制器:
public function searchIndeed(Request $request, IndeedQuery $query, IndeedProvider $client)
{
dump($query); // Returns a mockery object
dd($client); // Returns original object
}
测试:
为什么模拟IndeedQuery而不是模拟IndeedProvider?发现问题
如果您试图模拟一个不存在的类,mockry不会抛出错误。当我要求上课时,我在考试中犯了一个拼写错误
控制器
use JobApis\Jobs\Client\Providers\IndeedProvider;
试验
使用mockry时,如果类不存在,则不会出现错误。因此,如果没有解析mockry对象,请检查拼写。发现问题
如果您试图模拟一个不存在的类,mockry不会抛出错误。当我要求上课时,我在考试中犯了一个拼写错误
控制器
use JobApis\Jobs\Client\Providers\IndeedProvider;
试验
使用mockry时,如果类不存在,则不会出现错误。因此,如果没有解析mockry对象,请检查拼写
use JobApis\Jobs\Client\Provider\IndeedProvider; // Notice missing 's'