Laravel-Test-Helpers icon indicating copy to clipboard operation
Laravel-Test-Helpers copied to clipboard

Relationships with custom key names

Open matalina opened this issue 11 years ago • 2 comments

Below is the error I get.

1) PageTest::testPageHasOnePage
Mockery\Exception\NoMatchingExpectationException: No matching handler found for page::hasOne("Page", 
"parent_id"). Either the method was unexpected or its arguments matched no expected argument list for this method



C:\wamp\www\squigglemd\vendor\mockery\mockery\library\Mockery\ExpectationDirector.php:93
C:\wamp\www\squigglemd\app\models\Page.php:18
C:\wamp\www\squigglemd\vendor\way\laravel-test-helpers\src\Way\Tests\ModelHelpers.php:59
C:\wamp\www\squigglemd\vendor\way\laravel-test-helpers\src\Way\Tests\ModelHelpers.php:36
C:\wamp\www\squigglemd\app\tests\PageTest.php:34

For the following model:

class Page extends Post {
    protected $guarded = array();

    protected $table = 'pages';

    public static $rules = array(
        'slug' => 'required|unique:posts',
        'title' => 'required',
        'user_id' => 'required|integer',
        'parent_id' => 'required|integer', 
        'order' => 'required|integer'
    );

    public function pages()
    {
        return $this->hasOne('Page', 'parent_id');
    }
}

And the test code:

    protected function _cleanUp()
    {
        $app = $this->createApplication();

        $app->make('artisan')->call('migrate:refresh');
        //Artisan::call('migrate:refresh');

        $page = new page();
        $page->slug = 'testing';
        $page->title = 'Testing';
        $page->user_id = 1;
        $page->parent_id = 0;
        $page->order = 0;

        $this->page = $page;

    }

    /** Relationships **/

    public function testPageHasOnePage()
    {
        $this->_cleanUp();

        $this->assertHasOne('pages', 'page');
    }

It passes when I remove the parent_id from the hasOne relationship.

matalina avatar May 30 '13 21:05 matalina

I also have this issue. It appears to come from Mockery only expecting one piece of information back, however I have no idea how to add "optional withs" to mockery which I imagine would possible fix it.

sorora avatar Jun 01 '13 00:06 sorora

https://github.com/sorora/Laravel-Test-Helpers/commit/4658c228959f03ca46a4c8c5bdfdfb2f34fe65b4 ... unfortunately I seem to stupid to of applied it in this issue thread. A proposed fix has been found though :-).

sorora avatar Jun 01 '13 00:06 sorora