Search code examples
phpunit-testinglaravelbddbehat

How to test registration form in Laravel 5 with Behat ? How to write a feature?


I have to test my Larravel application using Behat ,and I have written Laravel application that satisfies:

A user opens the web application in his/her browser

The user enters his/her name and email into the registration form

The user clicks the 'Register' button below the form

The application registers the user (name/email) in the database

The application redirects the user to a page that contains a “Thank you” message.***

I have to write Behat features ,but I don-t know where to start? I have read about BDD but still confused.

Also it should be covered with unit tests for basic things like non-UTF-8 input, empty forms and non-string input.

Something like this?

    Feature: Registation form
        In order to test register on this site
        As a visitor
        I need to fill in the necessary info
    Scenario: 
        Given I am on the homepage
        When I fill in "username" with "Chrismo" 
        And I fill in "email" with "chris@chrismo.com"
        Then I should see "THank you"

Solution

  • You will need to write all your feature files on the features folder.

    For example, if you want to write a feature for functional testing, you can create a file named welcome.feature and put it in features/functional folder.

    And for Unit testing, Behat doesn't have assertions per-se. So, you may want to use PHPUnit's assertions. As PHPUnit comes bundled with new Laravel apps, it's already available, and all one needs to do to access the assertions is import the class in the FeatureContext class of Behat, as follows:

    use PHPUnit_Framework_Assert as PHPUnit;

    You will then have access to assertions and write your tests.

    EDIT: Here's a detailed tutorial which you can follow: https://blog.vinelab.net/building-reliable-apis-using-bdd-behant-laravel-5-949156a37e66#.yv89tcqkw