【问题标题】:i want to test a method in a repository with events phpunit laravel我想用事件 phpunit laravel 在存储库中测试一个方法
【发布时间】:2018-09-28 08:59:58
【问题描述】:

大家好,我是单元测试的新手,我一直坚持这一点

这是我正在测试的存储库方法 =>

public function store(array $input){

        $input['billing_contact'] = (isset($input['billing_contact'])) ? 1 : 0;  
        \DB::beginTransaction();
        try {
            $new_contact = $this->model->create($input);

            //EVENT CREATE ACCOUNT (USER)
            event(new EventCreateUserWithContact($new_contact));
            \DB::commit();
        }
        catch(\Exception $e){
            \DB::rollback();
            return false;
        }


        return $new_contact;

    }

这是我正在尝试进行的测试 =>

class ContactTest extends TestCase
{
    use WithFaker;
    protected $contact;/**/
    protected $container;/**/

    public function setup()
    {
        parent::setup();
        $this->container = new \Illuminate\Container\Container();
        $this->contact = new Contact();
        DB::beginTransaction();
    }
    public function tearDown()
    {
        DB::rollback();
        parent::tearDown();
    }
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testContactRepo()
    {
//        Event::();
        $publisher = factory(\App\Models\Publisher::class)->create();
        $contact =  factory(\App\Models\Contact::class)->create();


//
        $data =  [

            'first_name' => $this->faker->word,
            'last_name'=>  $this->faker->word,
            'email' => $this->faker->unique()->safeEmail,
            'phone'=> 112321321,
            'job'=> $this->faker->word,
            'billing_contact'=> $this->faker->word,
            'approve_delete_at'=> $this->faker->date('Y-m-d',  '1461067200'),
            'publisher_id'=> $publisher->id,

        ];

        $rep = new ContactRepositoryEloquent($this->container);

        $contact = $rep->store($data);
    dd($contact);


    }
}

我不明白如何获取新的联系人对象,因为我在运行测试时遇到异常错误(我得到错误) 我是否需要创建一个虚假事件才能使其正常工作?

【问题讨论】:

  • 嗨。您应该将代码添加到您的问题中,而不是链接到它的图像。
  • 在这里复制你的代码
  • 完成了伙计们 ;) @Jonathon

标签: php laravel unit-testing phpunit repository


【解决方案1】:

我使用 =>

找到了解决方案

\Illuminate\Support\Facades\Event::fake();

它有助于通过事件并继续测试!

【讨论】:

    猜你喜欢
    • 2016-07-28
    • 2017-08-16
    • 2017-03-05
    • 2015-07-21
    • 2016-10-19
    • 2020-04-10
    • 2015-06-15
    • 2021-09-14
    • 2011-12-26
    相关资源
    最近更新 更多