簡體   English   中英

使用Laravel 5的Mockery模擬類

[英]Mocking a class using Mockery with Laravel 5

我正在努力讓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交互的庫在這里 我是Mockery的新手,我無法看到它來模擬相關的庫-當我運行測試時,推送請求仍然有效,並且由於未看到預期的調用而導致測試失敗。 我究竟做錯了什么?

在控制器代碼中,您需要從Laravel容器中獲取PushWoosh\\PushWoosh的實例,而不是實例化您自己的實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM