测试将文件上传到 Behat 功能文件中的表单

crm*_*cco 1 testing bdd symfony behat

我对编写 Behat 测试套件还很陌生,目前我正在尝试通过添加测试来充实我现有的功能文件来测试上传的文件。

这是我到目前为止所想出的。

  Scenario: Submitting a valid asset form and uploading a file
    When I submit a asset form with values:
      | name               | type  | position | active | file                                 |
      | St Andrews Release | image | 1        | 1      | /web/images/product/icon/default.jpg |
    Then the form should be valid
    And the entity form entity should have the following values
      | name               | type  | position | active | file                                 |
      | St Andrews Release | image | 1        | 1      | /web/images/product/icon/default.jpg |
      Failed asserting that null matches expected '/web/images/product/icon/default.jpg'.
    And the entity form entity should be persisted correctly
Run Code Online (Sandbox Code Playgroud)

这是处理该场景的方法:

   /**
     * @When I submit a asset form with values:
     */
    public function iSubmitAssetFormWithValues(TableNode $table)
    {
        $data       = $table->getColumnsHash()[0];
        $this->form = $this->submitTheForm('crmpicco.asset.type', $this->entity, $data);
    }
Run Code Online (Sandbox Code Playgroud)

submitTheForm方法返回一个Symfony\Component\Form\FormInterface.

我的做法正确吗?我目前收到错误:

无法断言 null 与预期的“/web/images/product/swatch/default.jpg”匹配。

Ben*_*der 6

我建议您在应用程序根目录中为将在行为测试中使用的文件创建专用的文件夹结构,因为测试和测试中使用的文件对于所有开发人员都必须一致。我有时会看到人们编写测试来上传本地桌面上存在的文件:)我的桌面和您的桌面会有所不同,因此测试会失败。

结构

football    #your application name/root
   build
      dummy
         document
            hello.doc
            world.xls
         image
            test.jpg
Run Code Online (Sandbox Code Playgroud)

行为.yml

除了其他常见设置之外,您还必须定义file_path.

....
....

default:
    extensions:
        Behat\MinkExtension\Extension:
            files_path: %behat.paths.base%/build/dummy/
....
....
Run Code Online (Sandbox Code Playgroud)

小黄瓜场景示例

Feature: I can upload a file which is stored in my generic "dummy" folder

  Scenario: I can upload image
    Given I am on "/"
    When I attach the file "image/test.jpg" to "league_flag"
    And I press "Submit"
    Then I should see "Succeeded."
Run Code Online (Sandbox Code Playgroud)