2016-04-23 17 views
0

なければならない私は、このような設定ファイルを有する:symfonyの3依存性注入 - 型エラー:(__constructに渡される引数1)インスタンス

SRC/AppBundle/services.yml

#imports: 
    # - { resource: '../../app/config/config.yml' } 

parameters: 
    #laikinas, tikras yra config.yml 
    app_url: http://app.guru 


services: 

    UserManagement: 
     class: Tests\AppBundle\SharedCode\UserManagement\UserManagement 
     arguments: [%app_url%] 

    UserRegistrationContext: 
     class: Tests\AppBundle\features\user_registration\bootstrap\UserRegistrationContext 
     arguments: ['@UserManagement'] 

をテスト/ AppBundle /機能/ user_registration /ストラップ/ UserRegistrationContext.php

<?php 

namespace Tests\AppBundle\features\user_registration\bootstrap; 

use Behat\Behat\Tester\Exception\PendingException; 
use Behat\Behat\Context\Context; 
use Behat\Behat\Context\SnippetAcceptingContext; 
use Behat\Gherkin\Node\PyStringNode; 
use Behat\Gherkin\Node\TableNode; 
use Tests\AppBundle\SharedCode\UserManagement\UserManagement; 

use AppBundle\Controller\UserController; 
use Tests\AppBundle\features\BaseContext; 

/** 
* Defines application features from the specific context. 
* 
* To run: 
* sudo vendor/behat/behat/bin/behat 
* tests/AppBundle/features/user_registration/user_registration.feature 
* --stop-on-failure 
*/ 
class UserRegistrationContext extends BaseContext implements Context, SnippetAcceptingContext 
{ 
    private $userManagement; 

    /** 
    * UserRegistrationContext constructor. 
    */ 
    public function __construct(UserManagement $userManagement) 
    { 
     //$this->userManagement = new UserManagement(); 
     $this->userManagement = $userManagement; 
     parent::__construct(); 
    } 
} 

私はbehatテストを実行し、エラーを取得:

[email protected]:/shared$ sudo vendor/behat/behat/bin/behat tests/AppBundle/features/user_registration/user_registration.feature 

Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Tests\AppBundle\features\user_registration\bootstrap\UserRegistrationContext::__construct() must be an instance of Tests\AppBundle\SharedCode\UserManagement\UserManagement, none given in /shared/tests/AppBundle/features/user_registration/bootstrap/UserRegistrationContext.php:30 
Stack trace: 
#0 [internal function]: Tests\AppBundle\features\user_registration\bootstrap\UserRegistrationContext->__construct() 
#1 /shared/vendor/behat/behat/src/Behat/Behat/Context/ContextFactory.php(123): ReflectionClass->newInstance() 
#2 /shared/vendor/behat/behat/src/Behat/Behat/Context/ContextFactory.php(80): Behat\Behat\Context\ContextFactory->createInstance(Object(ReflectionClass), Array) 
#3 /shared/vendor/behat/behat/src/Behat/Behat/Context/Environment/Handler/ContextEnvironmentHandler.php(104): Behat\Behat\Context\ContextFactory->createContext('Tests\\AppBundle...', Array) 
#4 /shared/vendor/behat/behat/src/Behat/Testwork/Environme in /shared/tests/AppBundle/features/user_registration/bootstrap/UserRegistrationContext.php on line 30 

私は、services.ymlでパラメータを指定していることがわかります。なにが問題ですか?読むべきservices.ymlについては

、私はexctensionクラスを必要と理解して、ここにある:

のsrc/AppBundle /依存性の注入/ AppExtension.php

<?php 

namespace AppBundle\DependencyInjection; 

use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; 
use Symfony\Component\HttpKernel\DependencyInjection\Extension; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\Config\FileLocator; 

class AppExtension extends Extension 
{ 
    /** 
    * @param array   $configs configs 
    * @param ContainerBuilder $container container 
    * @return null 
    */ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     // ... you'll load the files here later 
     $loader = new YamlFileLoader(
      $container, 
      new FileLocator(__DIR__ . '/../') 
     ); 
     $loader->load('services.yml'); 
    } 
} 
+1

あなたは[この拡張子](https://github.com/Behat/Symfony2Extension/blob/master/doc/index.rst)を使用していますか? – Matteo

+0

私はそれを作曲家に追加しました。私はそれを使用しているかどうかはわかりません:D私はインストールする以外には何もしません。だから見てみる必要があります –

答えて

0

は、そんなにマッテオありがとう彼のコメントだけで問題は解決できました。

Symfony2 Extensionを使って、設定の代わりにbehat.ymlの依存関係を設定しなければならないことが判明しました。

default: 
    autoload: 
    '': %paths.base%/tests/AppBundle/features/user_registration/bootstrap 
    formatters: 
     progress: ~ 
    suites: 
    app_features: 

     paths: [ %paths.base%//tests/AppBundle/features ] 
     contexts: 
     - Tests\AppBundle\features\user_registration\bootstrap\UserRegistrationContext: 
      userManagement: '@UserManagement' 
     - Tests\AppBundle\features\user_login\bootstrap\UserLoginContext: 
      userManagement: '@UserManagement' 
     - Tests\AppBundle\features\password_reset\bootstrap\PasswordResetContext: 
      userManagement: '@UserManagement' 
    extensions: 
    Behat\Symfony2Extension: ~ 

そして私もSRC/AppBundle/services.ymlからUserManagementをコメントアウトし、それがどういうわけか、私はどのように実際に理解していない検索します。ここでbehat.ymlは今どのように見えるかです。ここ

そして、このことについて書かれたものです私は問題は解決した後、私は依存性注入behat symfony3のために再度ググ: http://docs.behat.org/en/v3.0/cookbooks/1.symfony2_integration.html

私は前にこのページを見て覚えているが、これを解決するとき、これは私の頭の中ではなかったが問題。おそらく、この例ではセッションが注入されていたので、symfonyコンポーネントですが、UserManagementクラスは作成されたコンポーネントでした。

更新:

最初から完了し、それらがどのように見えるかのファイルの最小限のバージョンが得られます:

behat.ymlは、プロジェクトのルートにしておく必要があります。 http://docs.behat.org/en/v3.0/cookbooks/1.symfony2_integration.html

default: 
    suites: 
     default: 
      contexts: 
       - FeatureContext: 
        userRepository: "@user_repository" 
    extensions: 
     Behat\Symfony2Extension: ~ 

機能/ストラップ/ FeatureContext.php

use AppBundle\Repository\UserRepository; 
use Behat\Behat\Tester\Exception\PendingException; 
use Behat\Behat\Context\Context; 
use Behat\Behat\Context\SnippetAcceptingContext; 
use Behat\Gherkin\Node\PyStringNode; 
use Behat\Gherkin\Node\TableNode; 

/** 
* Defines application features from the specific context. 
*/ 
class FeatureContext implements Context, SnippetAcceptingContext 
{ 
    private $userRepository; 

    /** 
    * Initializes context. 
    * 
    * Every scenario gets its own context instance. 
    * You can also pass arbitrary arguments to the 
    * context constructor through behat.yml. 
    */ 
    public function __construct(UserRepository $userRepository) 
    { 
     $this->userRepository = $userRepository; 
    } 

    //other methods 
} 

ご注意キーの一致がなければならないこと - そのようなキーがある場合:コンストラクタで、その後

userRepository: "@user_repository" 

変数名は

$userRepository