【问题标题】:Mocking a class using Mockery with Laravel 5使用 Mockery 和 Laravel 5 模拟一个类
【发布时间】:2016-01-14 23:02:28
【问题描述】:

我正在努力让 Mockery 与特定的 Laravel 5 项目一起工作。这是我写的测试:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Group;
use App\Dealer;
use App\News;
use App\User;

class PushControllerTest extends ControllerTestCase
{
    public function testCreatingPush()
    {   
        // Set user
        $this->be($this->user);

        // Mock Pushwoosh
        $this->mock = \Mockery::mock('PushWoosh\PushWoosh', array('createMessage' => true));
        $this->app->instance('PushWoosh\PushWoosh', $this->mock);
        $this->mock->shouldReceive('createMessage')->once()->andReturn(true);

        // Put together the data
        $data = array(
            'msg' => 'My first push notification'
        );

        // Send a POST request
        $response = $this->call(
            'POST',
            '/admin/api/v1/pushes',
            $data,
            array(),
            array(),
            array()
        );
        $this->assertResponseStatus(200);
    }   
}

这里是有问题的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Dealer;

use Config;

class PushController extends CustomBaseController
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        // Get group for user
        $group = $this->user->group;

        // Send a Push notification
        $pushwoosh = new \PushWoosh\PushWoosh(
            // @codeCoverageIgnoreStart
            Config::get('app.pushwoosh_application'),
            '',
            '',
            Config::get('app.pushwoosh_apitoken')
            // @codeCoverageIgnoreEnd
        );

        // Validate content
        $valid = $this->validate($request, [
            'msg' => 'required'
        ]);

        // Get message and target (if any)
        $msg = $request->input('msg');
        $target = $request->input('target');

        // Send push notification
        if ($target) {
            $users = User::where('dealer_id', $target)->get();
        } else {
            $users = User::where('group_id', $group->id)->get();
        }
        $pushesToSend = array();
        foreach($users as $user) {
            // Send push notifications for this user
            $push = array();
            $push['content'] = $msg;

            // Define conditions
            $push['conditions'] = ['UserID', 'EQ', $user->id];

            // Add it to the list
            array_push($pushesToSend, $push);
        }

        // Send pushes
        $response = $pushwoosh->createMessage($pushesToSend);

        // Respond
        if ($response) {
            return response()->json(200);
        } else {
            return response()->json(400);
        }
    }
}

现在,我正在使用 Pushwoosh 从管理仪表板发送推送消息。我用来与 Pushwoosh 交互的库是 here。我是 Mockery 的新手,我看不到它来模拟有问题的库 - 当我运行测试时推送请求仍然存在,并且测试失败,因为它没有看到预期的调用。我做错了什么?

【问题讨论】:

    标签: php laravel laravel-5 mockery


    【解决方案1】:

    在控制器代码中,您需要从 Laravel 容器中获取 PushWoosh\PushWoosh 的实例,而不是实例化您自己的实例。

    【讨论】:

    • 您能详细说明一下吗?
    • 最终解决了这个问题 - 必须为 Pushwoosh 库创建一个服务提供者,并将其注入控制器。之后一切都按预期进行。
    猜你喜欢
    • 2016-08-17
    • 2014-11-05
    • 2017-03-02
    • 2015-03-27
    • 2015-04-09
    • 2022-01-04
    • 2014-12-04
    • 2021-06-23
    • 2016-07-22
    相关资源
    最近更新 更多