【问题标题】:How can I specify Partial Mockup for a Laravel Facade?如何为 Laravel Facade 指定 Partial Mockup?
【发布时间】:2014-02-02 03:21:26
【问题描述】:

首先我尝试了这个

$mock = m::mock('Cartalyst\Sentry\Facades\Laravel\Sentry');
$mock->shouldReceive('getUser')->once()->andReturn($userInst);

但它给了我

Fatal error: Cannot redeclare Mockery_1964315998_Cartalyst_Sentry_Facades_Laravel_Sentry::shouldReceive()

然后我发现 Laravel Facades 已经实现了 Mockery,所以我直接在 Facade 上尝试了。

Sentry::shouldReceive('getUser')->once()->andReturn($userInst);

但现在的问题是它没有找到该对象的其他功能,基本上我需要部分模型的行为,但我不知道如何告诉它。

BadMethodCallException: Method Mockery_2115409749_Cartalyst_Sentry_Sentry::check() does not exist on this mock object

这是我需要的

A traditional partial mock defined ahead of time which methods of a class are to be mocked and which are to left unmocked (i.e. callable as normal). The syntax for creating traditional mocks is:

$mock = \Mockery::mock('MyClass[foo,bar]');
In the above example, the foo() and bar() methods of MyClass will be mocked but no other MyClass methods are touched. You will need to define expectations for the foo() and bar() methods to dictate their mocked behaviour.

【问题讨论】:

    标签: laravel mockery cartalyst-sentry


    【解决方案1】:

    你可以尝试添加

    Sentry::getFacadeRoot()->makePartial();
    

    之后

    Sentry::shouldReceive('getUser')
          ->once()
          ->andReturn($userInst);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 2018-04-29
      相关资源
      最近更新 更多