単体テストについて学んでいて、次の問題を解決しようとしました。
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for zfcUserAuthentication
...で与えられた唯一の答えを使用して:
ZfcUser を使用したコントローラーの単純な ZF2 単体テスト
したがって、私のsetUp関数は同じように見えます。残念ながら、次のエラー メッセージが表示されます。
Zend\Mvc\Exception\InvalidPluginException: Plugin of type Mock_ZfcUserAuthentication_868bf824 is invalid; must implement Zend\Mvc\Controller\Plugin\PluginInterface
コードのこの部分で発生します(私のコードでも同じように分割されています):
$this -> controller->getPluginManager()
->setService('zfcUserAuthentication', $authMock); // Error refers to this line.
$authMock オブジェクトは、setService に渡すために実装する必要がある plugininterface を実装していないようです。
$authMock は、単体テストで使用するためにそこに渡されることを意図していませんか? 別の (単体テスト指向の) setService メソッドを使用する必要がありますか?
アプリケーションへのログインを処理する方法が必要です。そうしないと、単体テストが無意味になります。
アドバイスをありがとう。
=== 編集 (2013/11/02) ===
これが問題の領域だと思うので、明確にするためにこの部分に焦点を当てたいと思いました:
// Getting mock of authentication object, which is used as a plugin.
$authMock = $this->getMock('ZfcUser\Controller\Plugin\ZfcUserAuthentication');
// Some expectations of the authentication service.
$authMock -> expects($this->any())
-> method('hasIdentity')
-> will($this->returnValue(true));
$authMock -> expects($this->any())
-> method('getIdentity')
-> will($this->returnValue($ZfcUserMock));
// At this point, PluginManager disallows mock being assigned as plugin because
// it will not implement plugin interface, as mentioned.
$this -> controller->getPluginManager()
->setService('zfcUserAuthentication', $authMock);
モックが必要な実装を処理しない場合、他にどのようにログインするふりをすることができますか?