【问题标题】:is it possible to use PHpspec with behat?, or just PhPunit with behat? (Symfony)是否可以将 PHpspec 与 behat 一起使用?或者仅将 PhPunit 与 behat 一起使用? (交响乐)
【发布时间】:2014-06-19 20:43:36
【问题描述】:

这些天我在玩 Behat .就我而言,我使用的是 Symfony。

如果没有,我如何在 Behat 中使用 PHPunit 中的断言。我现在不能,我用 composer 安装了 PHPUnit,但是在 Behat 文档中他们需要一个文件,但是场景略有不同(他们没有使用 composer 安装它,不在 Vendor 目录中)

来自 DOC 的网址:http://docs.behat.org/quick_intro.html#more-about-features

,require_once 有点优雅。

任何想法?,任何答案?,谢谢!

【问题讨论】:

    标签: symfony phpunit behat phpspec


    【解决方案1】:

    您可以使用the expect helper 来使用phpspec 的匹配器。

    几个使用例子:

    // matches if $something === 'something'
    expect($something)->toBe('something');
    
    // matches if $article->isActive() returns true
    expect($article)->toBeActive(); 
    
    // matches if $article->hasComments() returns false
    expect($article)->notToHaveComments();
    

    【讨论】:

      【解决方案2】:

      您当然可以将 Bahat 与 PHPUnit 一起使用。我没有尝试使用 PHPSpec,但我敢肯定,如果您可以访问执行断言的逻辑,这是可能的。通常,任何测试框架都会为“外部用户”公开该逻辑。

      /**
       * @Then /^the magic should happen$/
       */
      public function assertUnicornsExist()
      {
          // Make standard assertions through static calls…
          PHPUnit_Framework_TestCase::assertTrue(true);
      }
      

      不确定我是否了解安装的全貌,但如果您使用 composer,您可以简单地配置“自动加载”部分以包含您的源和上下文(如果它们是分开的)。我的工作几乎是开箱即用的:

      {
          "require": {
              "behat/behat": "dev-master",
              "behat/mink": "dev-master",
              "behat/mink-extension": "dev-master",
              "behat/mink-browserkit-driver": "dev-master",
              "behat/mink-goutte-driver": "dev-master",
              "phpunit/dbunit": "*"
          },
          "autoload": {
              "psr-0": {
                  "": "src/"
              }
          }
      }
      

      然后您只需包含vendor/autoload.php。我建议添加一个使用 @BeforeSuite 钩子运行一次的引导程序。

      /**
       * @BeforeSuite
       */
       public static function setUpSuite(BeforeSuiteScope $scope)
       {
           // if not bootstrapped already, then include bootstrap with init'ing the autoloader, etc…
       }
      

      另外,如果您从 Behat 3 开始 - 文档还没有,请使用 http://behat.readthedocs.org/en/latest/。它们包含最新的必需品和一些不错的示例。

      【讨论】:

      • Jumm,有趣,我会看看这个,谢谢你的回答!
      • 如果您的 Behat 上下文类是命名空间的,则必须在对 PHPUnit_Framework_TestCase 的调用前加上反斜杠以引用全局命名空间。
      【解决方案3】:

      当然可以。您可以使用 behat 来描述某些事物的行为方式,并使用 phpspec 来描述更详细的事物。很好的例子在这里:http://code.tutsplus.com/tutorials/a-bdd-workflow-with-behat-and-phpspec--cms-21601

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-14
        相关资源
        最近更新 更多