简体   繁体   English

使用Laravel 5的Mockery模拟类

[英]Mocking a class using Mockery with Laravel 5

I'm struggling to get Mockery working with a particular Laravel 5 project. 我正在努力让Mockery与特定的Laravel 5项目合作。 Here's the test I've written: 这是我编写的测试:

<?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);
    }   
}

And here's the controller in question: 这是有问题的控制器:

<?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);
        }
    }
}

Now, I'm using Pushwoosh to send push messages from an admin dashboard. 现在,我正在使用Pushwoosh从管理仪表板发送推送消息。 The library I'm using to interface with Pushwoosh is here . 我用来与Pushwoosh交互的库在这里 I'm new to Mockery and I can't see to get it to mock the library in question - the push request still goes when I run the tests and the test fails as it didn't see the expected call. 我是Mockery的新手,我无法看到它来模拟相关的库-当我运行测试时,推送请求仍然有效,并且由于未看到预期的调用而导致测试失败。 What am I doing wrong? 我究竟做错了什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM