Mockery1.0 Not Calling the Original Constructor

Not Calling the Original Constructor

When creating generated partial test doubles, Mockery mocks out only the method which we specifically told it to. This means that the original constructor of the class we are mocking will be called.

In some cases this is not a desired behavior, as the constructor might issue calls to other methods, or other object collaborators, and as such, can create undesired side-effects in the application's environment when running the tests.

If this happens, we need to use runtime partial test doubles, as they don't call the original constructor.

class MyClass
{
    public function __construct()
    {
        echo "Original constructor called." . PHP_EOL;
        // Other side-effects can happen...
    }
}

// This will print "Original constructor called."
$mock = \Mockery::mock('MyClass[foo]');

A better approach is to use runtime partial doubles:

class MyClass
{
    public function __construct()
    {
        echo "Original constructor called." . PHP_EOL;
        // Other side-effects can happen...
    }
}

// This will print "Original constructor called."
$mock = \Mockery::mock('MyClass')->makePartial();
$mock->shouldReceive('foo');

This is one of the reason why we don't recommend using generated partial test doubles, but if possible, always use the runtime partials.

Read more about creating-test-doubles-partial-test-doubles.

Note

The way generated partial test doubles work, is a BC break. If you use a really old version of Mockery, it might behave in a way that the constructor is not being called for these generated partials. In the case if you upgrade to a more recent version of Mockery, you'll probably have to change your tests to use runtime partials, instead of generated ones.

This change was introduced in early 2013, so it is highly unlikely that you are using a Mockery from before that, so this should not be an issue.

ドキュメント章別ページ

ヘッダー項目移動

注目:アイコン:ページ内リンク設置(リンクがないヘッダーへの移動では、リンクがある以前のヘッダーのハッシュをURLへ付加します。

移動

クリックで即時移動します。

バージョン

設定

適用ボタンクリック後に、全項目まとめて適用されます。

カラーテーマ
和文指定 Pagination
和文指定 Scaffold
Largeスクリーン表示幅
インデント
本文フォント
コードフォント
フォント適用確認

フォントの指定フィールドから、フォーカスが外れると、当ブロックの内容に反映されます。EnglishのDisplayもPreviewしてください。

フォント設定時、表示に不具合が出た場合、当サイトのクッキーを削除してください。

バックスラッシュを含むインライン\Code\Blockの例です。

以下はコードブロックの例です。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * ユーザに関連する電話レコードを取得
     */
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}

設定を保存する前に、表示が乱れないか必ず確認してください。CSSによるフォントファミリー指定の知識がない場合は、フォントを変更しないほうが良いでしょう。

キーボード・ショートカット

オープン操作

PDC

ページ(章)移動の左オフキャンバスオープン

HA

ヘッダー移動モーダルオープン

MS

移動/設定の右オフキャンバスオープン

ヘッダー移動

T

最初のヘッダーへ移動

E

最後のヘッダーへ移動

NJ

次ヘッダー(H2〜H4)へ移動

BK

前ヘッダー(H2〜H4)へ移動

その他

?

このヘルプページ表示
閉じる