小编too*_*vdb的帖子

如何 Str::shouldReceive?(模拟 Illuminate\Support\Str)

我有一个Str::random()我想测试的类。

但是当我Str::shouldReceive('random')在我的测试中使用时,我得到一个 BadMethodCallException 说该方法 shouldReceive 不存在。

我还尝试直接模拟该类并将其绑定到 IOC,但它一直在执行原始类,生成一个随机字符串而不是我在模拟上设置的返回值。

    $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');
Run Code Online (Sandbox Code Playgroud)

unit-testing facade laravel mockery

5
推荐指数
1
解决办法
1249
查看次数

标签 统计

facade ×1

laravel ×1

mockery ×1

unit-testing ×1