in Education by
I'm trying to test my Category class. I'm using Mockery::mock() method, with 'overload:' prefix and makePartial() method. When running test I have this error: Mockery\Exception\BadMethodCallException : Method App\Models\Category::getDynamicFieldsForDocument() does not exist on this mock object Here is my code: namespace App\Models; class Category extends DictionaryBase{ //some methods public function getDynamicFieldsForDocument() { $data = []; $parents = []; $allParents = $this->getParents($this->id, $parents); foreach ($allParents as $parentId) { $parent = Category::find($parentId); $fields = $parent->dynamicFields(); foreach ($fields as $field) { $data[$field['name']] = $field; } } return $data; } } TestCase: namespace Tests\Unit; use App\Models\Category; use Tests\TestCase; class CategoryModelTest extends TestCase{ //some methods /** * @runInSeparateProcess * @preserveGlobalState disabled */ public function testGetDynamicFieldsForDocument() { $mockCategory = \Mockery::mock('overload:'.Category::class)->makePartial(); $preparedDynamicFields = $this->prepareDynamicFields(); $preparedCategories = $this->prepareCategories(); $mockCategory->shouldReceive('find')->andReturn($preparedCategories[0], $preparedCategories[1], $preparedCategories[2]); $mockCategory->shouldReceive('getParents')->andReturn(['1a2b', '3c4d', '5e6f']); $mockCategory->shouldReceive('dynamicFields')->andReturn(null, $preparedDynamicFields[0], $preparedDynamicFields[1]); $response = $mockCategory->getDynamicFieldsForDocument(); dd($response); } } I have no idea why i still have error. I think when ->makePartial() method is called it should mock only methods, which are called by ->shouldReceive() EDIT: Now I'm making mock instance without :overload, and mocking 'find' method in this way: `$mockCategory->shouldReceive('find')->andReturn($preparedCategories[0], $preparedCategories[1], $preparedCategories[2]);` My find method looks like this: public static function find($id) { return $id ? self::list(config(static::IDENT.'.fields'), (new Filter('and'))->add('id', $id, '')->toArray(),[],1,1)[0] ?? null : null; } And this is my error: Error : Wrong parameters for App\Exceptions\ApiException([string $message [, long $code [, Throwable $previous = NULL]]]) It's because list method call API so it looks like this method is called without mock. I know that i can't mock static method, but earlier when I used :overload it was possible. What's now? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Delete :overload and just define your mock as: $mockCategory = \Mockery::mock(Category::class)->makePartial() Example Model: namespace App\Models; class Foobar extends BaseModel { public function foonction() { Foobar::find(); return '1'; } } Test: namespace Tests; use Evalua\Heva\Models\Foobar; class FoobarTest extends TestCase { public function testFoobar() { $fooMock = \Mockery::mock('overload:'.Foobar::class)->makePartial(); $fooMock->shouldReceive('find')->once(); $fooMock->foonction(); } } Fails with: Mockery\Exception\BadMethodCallException: Method Evalua\Heva\Models\Foobar::foonction() does not exist on this mock object Without the :overload the test pass. The explanation should be based on what's written in the documentation about overload: Prefixing the valid name of a class (which is NOT currently loaded) with “overload:” will generate an alias mock (as with “alias:”) except that created new instances of that class will import any expectations set on the origin mock ($mock). The origin mock is never verified since it’s used an expectation store for new instances. For this purpose we use the term “instance mock”

Related questions

0 votes
    I'm trying to test my Category class. I'm using Mockery::mock() method, with 'overload:' prefix ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Which of the following method returns a geolocation object in HTML5? A - navigator.geolocation B - browser.geolocation C - API.geolocation D - None of the above....
asked Dec 3, 2020 in Technology by JackTerrance
0 votes
    We are trying to run automated tests using Microsoft Test Manager 2015. We've almost reached our goal. This ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I created a new Access Key and configured that in the AWS CLI with aws configure. It created the .ini file in ~/ ... . How to fix this? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    What is the best way to open a file as reading/write if it exists, or if it does not, then create it and ... to do the opening part. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
0 votes
    Which one of the following parameters does not exist for the two-port network in the circuit given below? (a) h ... for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 13, 2021 in Education by JackTerrance
0 votes
    Which of the following case does not exist in complexity theory (a) Best case (b) Worst case (c) ... upon Sorting in division Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    In my unit test I am calling the following: every { connectivityManager.activeNetworkInfo } returns null but ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am using Realm with Swift 3 in my iOS app. I have the following code //Find all records for the day func ... { let predicate = NSPredicate(format: "date >= %@ and date...
asked May 8, 2022 in Education by JackTerrance
0 votes
    It is a good practise to not throw which exception in close() method of autocloseable? (a) IOException (b) ... & API of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    If member does not implement serialization, which exception would be thrown? (a) RuntimeException (b) ... and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which is the correct code that returns a complex number that is the complex conjugate of this one? (a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
...