crmpicco
crmpicco

Reputation: 17181

Testing a file upload to a form in a Behat feature file

I am quite new to writing Behat test suites and I am currently trying to flesh out my existing feature file with an added test to test an uploaded file.

This is what I have come up with so far.

  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

This is the method handling the scenario:

   /**
     * @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);
    }

The submitTheForm method returns a Symfony\Component\Form\FormInterface.

Am I along the right lines? I am currently getting an error:

Failed asserting that null matches expected '/web/images/product/swatch/default.jpg'.

Upvotes: 1

Views: 6401

Answers (1)

BentCoder
BentCoder

Reputation: 12740

I suggest you to create a dedicated folder structure for files which will be used in behat tests right in your application root because tests and the files used in tests must be consistent for all developers. I sometimes see people writing tests to upload files that exist on their local desktop:) My desktop and your desktop would be different hence reason the test would fail.

Structure

football    #your application name/root
   build
      dummy
         document
            hello.doc
            world.xls
         image
            test.jpg

behat.yml

Apart from other common setting, you must define file_path.

....
....

default:
    extensions:
        Behat\MinkExtension\Extension:
            files_path: %behat.paths.base%/build/dummy/
....
....

Example Gherkin scenario

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."

Upvotes: 6

Related Questions