BEHAT Behaviordriven development Installing Install Behat and Mink
BEHAT Behavior-driven development
Installing ■ Install Behat and Mink: composer require behat/behat/mink-extension laracasts/behat-laravel-extension --dev alias behat='~/behat/vendor/behat/bin/behat' ■ Let’s make auth so we can demo a login page check php artisan make: auth ■ Create features directory: behat --init ■ See the definitions list: behat -dl
■ In Feature. Context. php import Mink. Context: use BehatMink. ExtensionContextMink. Context; extends Mink. Context
So what is Mink? ■ The Mink Context simulates a web browser ■ Gives you functions like i. Am. On. Home. Page(), visit(page), press. Button(button), fill. Field(field, value)
Behat config Create behat. yml and. env. behat for behat specific variables behat. yml default: extensions: LaracastsBehat: env_path: . env. behat BehatMink. Extension: default_session: laravel: ~. env. behat APP_ENV=acceptance APP_DEBUG=true DB_PATH=acceptance. sqlite
Let’s write some Behat! Create example. feature Feature: Testing In order to teach Behat As a teacher I want to demonstrate how to install and create features Scenario: Home Page Given I am on the homepage Then I should see "forge" Scenario: Dashboard is locked to guests When I go to "home" Then the URL should match "login"
Is it not working Josh? Use a gif to distract everyone and make the font really small so it’s not embarrassing when your demo didn’t work and you forgot why… Edit your. env to use localhost: 8888 then run php artisan config: cache and then when that breaks your whole site do php artisan config: clear because pugs
Another example: authentication Create authentication. feature Feature: Authentication Why (To give registered members exclusive content) Who (As an administrator) What (I need authentication and registration for users) Scenario: Registration When I register "Josh. La. Mar" "jolamar@iu. edu" Then I should have an account behat --append-snippets
Feature. Context snippets Open Feature. Context. php use PHPUnit_Framework_Assert as PHPUnit; public function i. Register($name, $email) { $this->visit('register'); $this->fill. Field('name', $name); $this->fill. Field('email', $email); $this->fill. Field('password', 'password'); $this->fill. Field('password_confirmation', 'password'); >press. Button('Register'); } public function i. Should. Have. An. Account() { $this->assert. Page. Address('home'); } $this- PHPUnit: : assert. True(Auth: : check());
SQLite database Open config/database. php 'default' => env('DB_CONNECTION', 'sqlite'), 'sqlite' => [ 'driver' => 'sqlite', 'database' => database_path( env('DB_PATH', 'database. sqlite') ), 'prefix' => '', ], touch database/acceptance. sqlite
Migrator Open features/bootstrap/Feature. Context. php use LaracastsBehatContextMigrator; use LaracastsBehatContextDatabase. Transactions; use Migrator, Database. Transactions;
Another example: login Open authentication. feature Scenario: Logging in Given I have an account "Josh La. Mar" "jolamar@iu. edu" When I sign in Then I should be logged in behat --append-snippets
Awesome links ■ Behat documentation: http: //behat. org ■ Behat Laravel Git. Hub: https: //github. com/laracasts/Behat-Laravel-Extension ■ Jeffrey Way’s version of this presentation: https: //laracasts. com/lessons/laravel 5 -and-behat-bffs
- Slides: 13