I have a class that uses Str::random()
which I would like to test.
But when I use Str::shouldReceive('random')
in my test, I get a BadMethodCallException saying the method shouldReceive does not exist.
I also tried to mock the class directly and bind it to the IOC but it keeps executing the original class, generating a random string and not the return value I set on the mock.
$stringHelper = Mockery::mock('Illuminate\Support\Str');
$this->app->instance('Illuminate\Support\Str', $stringHelper);
//$this->app->instance('Str', $stringHelper);
$stringHelper->shouldReceive('random')->once()->andReturn('some password');
//Str::shouldReceive('random')->once()->andReturn('some password');
you could not use laravel Mock because Str::random() is not Facade. instead, you can create a new class with a method that set your test environment. like this:
<?php
namespace App;
use Illuminate\Support\Str;
class Token
{
static $testToken =null;
public static function generate(){
return static::$testToken ?:Str::random(60);
}
public static function setTest($string){
static::$testToken = $string;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With