简体   繁体   English

Laravel 8.x:模拟 API 测试:错误:调用未定义的方法 App\Services\TrelloCardService::_downloadCardsFromBoard()

[英]Laravel 8.x: MOCK API test:Error: Call to undefined method App\Services\TrelloCardService::_downloadCardsFromBoard()

when i call the feature test i get the following error:Call to undefined method App\Services\TrelloCardService::_downloadCardsFromBoard().I don't understand the reason for the error当我调用功能测试时,我收到以下错误:调用未定义的方法 App\Services\TrelloCardService::_downloadCardsFromBoard()。我不明白错误的原因

I wrote the following service:我写了以下服务:

namespace App\Services;

use App\Services\Api\TrelloCardAPIService;

class TrelloCardService
{
    protected $trelloCardApiService;

    public function __construct(TrelloCardAPIService $trelloCardApiService)
    {
        $this->trelloCardApiService = $trelloCardApiService;
    }
    
}

then a service that calls the API:然后是调用 API 的服务:

<?php


namespace App\Services\Api;

use App\Traits\CardTrait;
use Unirest\Request;

class TrelloCardAPIService
{
    public function call(string $url) {
        $headers = array('Accept' => 'application/json');
        $query = array('key' => env('TRELLO_KEY'), 'token' => env('TRELLO_TOKEN'));
        $r = Request::get($url, $headers, $query);
        return $r->body;
    }

    public function _downloadCardsFromBoard() {
        echo "API downloadCards!\n";
        $url = TRELLO_API_BASE_URL . "/boards/".TRELLO_BOARDS_SPRINT."/cards";
        $res = $this->call($url);
        return $res;
    }

}

then I wrote the test feature:然后我写了测试功能:

namespace Tests\Feature;

use App\Services\Api\TrelloCardAPIService;
use App\Services\TrelloCardService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\File;

use Tests\TestCase;

class trelloCardTest extends TestCase
{
    public function test_mock_card()
    {
        $cards = json_decode(File::get("tests/test_data/cards.json"),FALSE);

        $mock = $this->mock(TrelloCardAPIService::class, function ($mock) use ($cards) {
            $mock->shouldReceive('_downloadCardsFromBoard')
                ->once()
                ->andReturn($cards);
        });
        
        //here I print the var mock if I do the DD
        $mockedTrelloCardService = new TrelloCardService($mock);

        $data = $mockedTrelloCardService->_downloadCardsFromBoard();//fail this
        dd($data);//I would like to print $cards
    }
}

give me following error:给我以下错误:

Error: Call to undefined method App\Services\TrelloCardService::_downloadCardsFromBoard()

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

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