19. In BDD, you write feature ïŹles in the form of user stories
that you test against.
20. BDD uses a ubiquitous language - basically, a language
that business stakeholders, project managers, developers
and our automated tools can all understand.
21. Sample Behat Feature File
Feature: Laravel Testâš
In order to demonstrate Laravel and Behatâš
As a userâš
I need to be able to visit the homepage of a new Laravel appâš
âš
Scenario: Homepageâš
Given I am on the homepageâš
Then I should see "Laravel 5"âš
45. features/ïŹtbit.feature
Feature: Fitbit Integrationâš
In order to obtain Fitbit dataâš
As a userâš
I need to be able to authenticate with Fitbitâš
Scenario: Not yet authenticatedâš
Given I am not logged in as âjosh@creatuity.comâ
When I go to "/fitbit/"âš
Then I should see "Please authenticate"âš
âš
46. vendor/bin/behat âappend-snippets
Scenario: Not yet authenticated:6
Given I am not logged in as âjosh@creatuity.com
When I go to "/fitbit/"
Then I should see "Please authenticate"
1 scenario (1 undefined)
3 steps (1 undefined, 2 skipped)
0m0.48s (11.00Mb)
u features/bootstrap/FeatureContext.php - `I am not logged in as`
definition added
50. Behat expects us to add logic to this function to
detect the user is not logged in.
51. Before we do that, letâs ïŹnish out our feature ïŹle.
52. features/ïŹtbit.feature continued
Scenario: I have authenticatedâš
Given I am logged in as âjosh@creatuity.comâ
When I go to "/fitbit/"âš
Then I should see "Welcome back"âš
Scenario: I have sleep dataâš
Given I am logged in as âjosh@creatuity.comââš
When I go to "/fitbit/sleep/"âš
Then I should see "Sleep Report"âš
55. /features/bootstrap/FeatureContext.php
/**âš
* @Given I am not logged in as :emailâš
*/âš
public function iAmNotLoggedInAs($email)âš
{âš
// We completely log outâš
// Destroy the previous sessionâš
if (Session::isStarted()) {âš
Session::regenerate(true);âš
} else {âš
Session::start();âš
}âš
}âš
56. /features/bootstrap/FeatureContext.php
public function iAmLoggedInAs($email)âš
{âš
// Destroy the previous sessionâš
if (Session::isStarted()) {âš
Session::regenerate(true);âš
} else {âš
Session::start();âš
}âš
âš
// Login the user and since the driver and this code nowâš
// share a session this will also login the driver sessionâš
$user = User::where('email', $email)->firstOrFail();âš
Auth::login($user);âš
âš
// Save the session data to disk or to memcacheâš
Session::save();âš
âš
// Hack for Seleniumâš
// Before setting a cookie the browser needs to be launchedâš
if ($this->getSession()->getDriver() instanceof BehatMinkDriverSelenium2Driver) {âš
$this->visit('login');âš
}âš
âš
// Get the session identifier for the cookieâš
$encryptedSessionId = Crypt::encrypt(Session::getId());âš
$cookieName = Session::getName();âš
âš
// Set the cookieâš
$minkSession = $this->getSession();âš
$minkSession->setCookie($cookieName, $encryptedSessionId);âš
}
58. vendor/bin/behat
âŠ
Scenario: I have sleep data
Given I am logged in as "josh@creatuity.com"
When I go to "/fitbit/sleep/"
Then I should see "Sleep Report"
The text "Sleep Report" was not found anywhere in the text of
the current page. (BehatMinkExceptionResponseTextException)
--- Failed scenarios:
features/fitbit.feature:6
features/fitbit.feature:11
features/fitbit.feature:16
71. vendor/bin/phpspec run
10 ! is initializable (142ms)
class Fitbit does not exist.
15 ! connects to fitbit (100ms)
class Fitbit does not exist.
20 ! returns sleep data
class Fitbit does not exist.
---- broken examples
Fitbit
10 ! is initializable (142ms)
class Fitbit does not exist.
Fitbit
15 ! connects to fitbit (100ms)
class Fitbit does not exist.
Fitbit
20 ! returns sleep data
class Fitbit does not exist.
1 specs
3 examples (3 broken)
76. This is very powerful with frameworks like Laravel
and Magento, which have PHPSpec plugins that
help PHPSpec know where class ïŹles should be
located.
77. Fitbit.php - class Fitbit {
function connect($email)âš
{âš
// TODO: write logic hereâš
}âš
âš
function sleepData($email)âš
{âš
// TODO: write logic hereâš
}âš