2014-05-13 14 views
5

Tình hìnhBehat 3 trong symfony2.4 (truy cập học thuyết)

Tôi muốn sử dụng BDD và Behat trong các dự án symfony tôi từ bây giờ. Dự án hiện tại là sf2.4, và tôi cố gắng làm cho Behat 3 hoạt động. Tôi sử dụng the latest doc concerning behat3, như được đề xuất bởi jakub trong this post.

vấn đề

Behat 3 dường như làm việc tốt. Tuy nhiên, để có thể thực sự bắt đầu, tôi cần phải có quyền truy cập vào Kernel (container, doctrine, etc ...). Tôi đã thử với my_project, thử nghiệm dự án tái tạo ví dụ ls cho Behat. Tuy nhiên, sử dụng $ this-> container(), $ this-> kernel-> getContainer() luôn đặt ra một 'cấp phát ngoại lệ' (mã dừng lại ở iShouldGet bước):

public function iShouldGet(PyStringNode $string) 
{ 
    //$container = $this->kernel->getContainer(); 
    //$container = $this->getContainer(); 
    //$doctrine = $this->getContainer()->get('doctrine'); 
    if ((string) $string !== $this->output) { 
     throw new Exception(
      "Actual output is:\n" . $this->output 
     ); 
    } 
} 

tôi đã cố gắng để tạo ra các behat cùng test 'ls' trong AcmeDemoBundle:

|Acme 
    |Demo 
     |Features 
      |Context/FeatureContext.php 
      ls.feature 

Tuy nhiên, nó đặt ra một lỗi:

[Behat\Testwork\Tester\Exception\WrongPathsException]  
No specifications found at path(s) `@AcmeDemoBundle`. 

giải pháp

Nó có thể là do sử dụng Behat3, nhưng tôi không chắc chắn. Bất kỳ gợi ý nào tại sao vấn đề này xảy ra/cách giải quyết nó? Nói chung, lời khuyên tốt về cách tích hợp ứng xử vào symfony2 (2.4) sẽ được đánh giá cao.

Cảm ơn rất nhiều trước.

Kính trọng,


NB: Đây là tác phẩm của tôi:

behat.yml

# behat.yml 
default: 
    suites: 
     my_suite: 
      type: symfony_bundle 
      bundle: AcmeDemoBundle 
      mink_session: default 
      mink_javascript_session: selenium2 
    extensions: 
     #Behat\MinkExtension\Extension: 
     #Behat\MinkExtension\ServiceContainer\MinkExtension: 
     Behat\MinkExtension: 
      base_url: 'http://demo.com' 
      # this will be the url of our application 
      #base_url: 'http://wikipedia.org' 
      sessions: 
       default: 
        goutte: ~ 
       selenium2: 
        selenium2: ~ 
     Behat\Symfony2Extension: ~ 

app/autoload.php

<?php 

use Doctrine\Common\Annotations\AnnotationRegistry; 
use Composer\Autoload\ClassLoader; 

/** 
* @var ClassLoader $loader 
*/ 
$loader = require __DIR__.'/../vendor/autoload.php'; 

AnnotationRegistry::registerLoader(array($loader, 'loadClass')); 


$loader->add('Behat\Gherkin',realpath(__DIR__.'/../vendor/behat/gherkin/src')); 
$loader->add('Behat\Behat' ,realpath(__DIR__.'/../vendor/behat/behat/src')); 
$loader->add('Behat\BehatBundle' ,realpath(__DIR__.'/../vendor/bundles')); 

return $loader; 

Trả lời

0

Với behat 3.0. 14, và symfony 2.3, tôi cần thêm se tuyên bố ở phía trên cùng của lớp FeatureContext:

use Behat\Symfony2Extension\Context\KernelAwareContext; 
use Behat\Symfony2Extension\Context\KernelDictionary; 

Các lớp đối tượng cũng cần thiết để thực hiện các giao diện KernelAwareContext như thế này:

class FeatureContext extends MinkContext implements KernelAwareContext 

Sau đó, tôi đã có thể truy cập vào thùng chứa symfony trong FeatureContext lớp học, như trong ví dụ step argument transfomation bên dưới:

/** 
* @Transform :location 
*/ 
public function castAddressToLocation($location) 
{ 
    return $this->getContainer()->get('geocoder')->getLocation($location); 
} 
2

Cho đến nay, điều này luôn làm việc cho tôi. Tôi đã thêm ví dụ sử dụng cho Gherkin của bạn vì vậy nó khá thẳng về phía trước.

use Behat\MinkExtension\Context\MinkContext; 
use Behat\Symfony2Extension\Context\KernelAwareContext; 
use Symfony\Component\HttpKernel\KernelInterface; 

class FeatureContext extends MinkContext implements KernelAwareContext 
{ 
    protected $kernel; 

    public function setKernel(KernelInterface $kernelInterface) 
    { 
     $this->kernel = $kernelInterface; 
    } 

    /** 
    * @Given /^I can access service container$/ 
    */ 
    public function iCanAccessServiceContainer() 
    { 
     $container = $this->kernel->getContainer(); 
     return $container->getParameter('whatever'); 
    } 

    /** 
    * @Given /^I can access entity manager$/ 
    */ 
    public function iCanAccessEntityManager() 
    { 
     $em = $this->kernel->getContainer()->get('doctrine')->getManager(); 
     // So on 
    } 

    /** 
    * @Given /^I can access repository$/ 
    */ 
    public function iCanAccessRepository() 
    { 
     $em = $this->kernel->getContainer()->get('doctrine')->getManager(); 
     $repo = $em->getRepository('WhateverBundle:WhateverEntity'); 
     // So on 
    } 
} 
Các vấn đề liên quan