Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

Laravel 8.x: MOCK API test:Error: Call to undefined method AppServicesTrelloCardService::_downloadCardsFromBoard()

when i call the feature test i get the following error:Call to undefined method AppServicesTrelloCardService::_downloadCardsFromBoard().I don't understand the reason for the error

I wrote the following service:

namespace AppServices;

use AppServicesApiTrelloCardAPIService;

class TrelloCardService
{
    protected $trelloCardApiService;

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

then a service that calls the API:

<?php


namespace AppServicesApi;

use AppTraitsCardTrait;
use UnirestRequest;

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!
";
        $url = TRELLO_API_BASE_URL . "/boards/".TRELLO_BOARDS_SPRINT."/cards";
        $res = $this->call($url);
        return $res;
    }

}

then I wrote the test feature:

namespace TestsFeature;

use AppServicesApiTrelloCardAPIService;
use AppServicesTrelloCardService;
use IlluminateFoundationTestingRefreshDatabase;
use IlluminateFoundationTestingWithFaker;
use IlluminateSupportFacadesFile;

use TestsTestCase;

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 AppServicesTrelloCardService::_downloadCardsFromBoard()
question from:https://stackoverflow.com/questions/65891925/laravel-8-x-mock-api-testerror-call-to-undefined-method-app-services-trelloca

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[2] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

56.9k users

...