首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Behat不调用扩展的mink方法

Behat不调用扩展的mink方法
EN

Stack Overflow用户
提问于 2015-08-06 21:33:12
回答 1查看 833关注 0票数 1

我自言自语地说:“我必须学会如何bdd?”我尝试过Symfony2 2/Behat/Mink组合。

嗯,我写代码是为了写教程。但是文档/教程中的每个behat版本都低于版本3。我想学习behat3。我要去behat的官方网页,并且阅读。

一切正常,但无论如何,behat cli应用程序不能调用扩展的mink类方法。我会把我的密码放在下面。我是如何用mink的方法运行behat3应用的。

特性/引导/FeatureContext.php

代码语言:javascript
复制
<?php

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  extends \Behat\MinkExtension\Context\MinkContext implements Context, SnippetAcceptingContext
{

}

特征/家.特征

代码语言:javascript
复制
Feature: Home
  I want to see homepage
  As a anonym user
  Scenario:
    Given |I am on |the homepage
    Then |I should see text matching "Sektör"

behat.yml

代码语言:javascript
复制
default:
  extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
      sessions:
        default:
          symfony2: ~
        selenium2:
          selenium2: ~
      base_url: http://site.dev

composer.json

代码语言:javascript
复制
    "behat/behat": "~3.0",
    "behat/mink-extension": "~2.0@dev",
    "behat/mink-goutte-driver": "~1.0",
    "behat/mink-selenium2-driver": "~1.1",
    "behat/symfony2-extension": "*"

结果:

代码语言:javascript
复制
Feature: Home
  I want to see homepage
  As a anonym user

  Scenario:                                   # features/home.feature:4
    Given |I am on |the homepage
    Then |I should see text matching "Sektör"

1 scenario (1 undefined)
2 steps (2 undefined)
0m0.12s (25.80Mb)

--- FeatureContext has missing steps. Define them with these snippets:

    /**
     * @Given |I am on |the homepage
     */
    public function iAmOnTheHomepage()
    {
        throw new PendingException();
    }

    /**
     * @Then |I should see text matching :arg1
     */
    public function iShouldSeeTextMatching($arg1)
    {
        throw new PendingException();
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-07 12:25:49

这将从版本3开始。找到完整的示例这里,在这里中有更多的示例,包括版本2。

composer.json

代码语言:javascript
复制
{
    "require-dev": {
        "behat/behat" : "3.0.15",
        "behat/symfony2-extension" : "2.0.0",
        "behat/mink": "1.6.1",
        "behat/mink-extension": "2.0.1",
        "behat/mink-browserkit-driver": "1.2.0",
        "behat/mink-goutte-driver": "1.1.0",
        "behat/mink-selenium2-driver": "1.2.0"
    }
}

behat.yml

代码语言:javascript
复制
default:
    extensions:
        Behat\Symfony2Extension: ~
        Behat\MinkExtension:
            base_url: http://football.local/app_test.php
            browser_name: firefox
            sessions:
                goutte: # fast, CLI, browser, no javascript support
                    goutte: ~
                selenium2: # fast, CLI, opens up a browser
                    selenium2: ~
                symfony2: # very fast, CLI, no browser
                    symfony2: ~
    suites:
        backend:
            type: symfony_bundle
            bundle: ApplicationBackendBundle
            mink_session: symfony2
            contexts:
                - Application\BackendBundle\Features\Context\FeatureContext:
                    param1: hello
                    param2: world

FeatureContext.php

代码语言:javascript
复制
namespace Application\BackendBundle\Features\Context;

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

class FeatureContext extends MinkContext implements KernelAwareContext
{
    private $kernel;
    private $param1;
    private $param2;

    public function __construct($param1, $param2)
    {
        $this->param1 = $param1;
        $this->param2 = $param2;
    }

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

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

示例特性

代码语言:javascript
复制
# Backend
Feature: Dummy feature

  Scenario: Home url
    Given I am on "/backend"
    Then I should see "Welcome to Application backend!"
    And I can access service container

# Frontend
Feature: Dummy feature

  Scenario: Home url
    Given I am on "/"
    Then I should see "Welcome to Application frontend!"
    And I can access service container

Run

bin/behat --suite=backend

代码语言:javascript
复制
Feature: Dummy feature

  Scenario: Home url
    Given I am on "/backend"
    Then I should see "Welcome to Application backend!"
    And I can access service container
      │ ThisTokenIsNotSoSecretChangeIt

1 scenario (1 passed)
3 steps (3 passed)
0m0.98s (28.17Mb)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31866173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档