如何嘲笑事件调用的特征中的类?

时间:2018-07-11 10:14:03

标签: laravel mockery

我无法在“更新”事件调用的特征类中模拟一个类。

在我的phpunit中:

public function setUp()
{
    ...

    $this->mock = \Mockery::mock(NewsletterGateway::class);

    app()->instance(NewsletterGateway::class, $this->mock);

    ...
}

public function test_method()
{
    ...
    $this->mock->shouldReceive('updateData')
               ->once();

    $privacy->update(['firstname' => 'Philipp']);
    ...
}

还有我的特质

namespace App\Newsletter;

use App\Newsletter\NewsletterGateway as Newsletter;


trait UpdatesNewsletter
{ 
    protected static function bootUpdatesNewsletter()
    {
       static::updated(function ($model){
          $model->updatesNewsletter();
       });
    }

    //called by the updated event
    protected function updatesNewsletter() 
    {
        $newsletter = new Newsletter; 

        dd($newsletter); //still the normal class     
    }

}

我实际上不确定如何模拟这个。一种方法是模拟父类,但这并不是我真正想要的(不想模拟其他方法名)。

在这种情况下,您将如何嘲笑?

1 个答案:

答案 0 :(得分:0)

对我有好处,但我需要更改代码并需要使用Jobs。因此,对其进行测试非常容易:

/** @test */
public function it_updates_newsletter_when_user_privacy_is_updated() 
{    
    Queue::fake();

    $privacy = create('App\UserPrivacy', ['user_id' => auth()->id()]);

    $privacy->update(['firstname' => 'Philipp']);

    Queue::assertPushed(ProcessUpdatesNewsletter::class);

}