How do I mock the "file_system" service?
You shouldn't call file_system with \Drupal::service('file_system') use dependency injection instead:

So try something like the following in your service:


Add arguments to your *.services.yml file.




services:
  yourModule.yourService:
    class: Drupal\yourModule\yourServiceClass
    arguments: ['@file_system']




Inject the file_system service in your Class:




  use Drupal\Core\File\FileSystemInterface;

  /**
   * The file system service.
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;
  /**
  * Class constructor.
  * @param \Drupal\Core\File\FileSystemInterface $file_system
  */
  public function __construct(FileSystemInterface $file_system) {;
    $this->fileSystem = $file_system;
  }




Change setBasePath method to be like.




  /**
   * @inheritdoc
   */
  public function setBasePath($path = Null) {

    if ($path !== Null) {
      $this->directories['base'] = $path;
    } else{
     $this->directories['base'] = $this->fileSystem->realpath(file_default_scheme() . "://");
    }
  }