Skip to content

Instantly share code, notes, and snippets.

@jamiejackson
Created May 5, 2016 19:52
Show Gist options
  • Select an option

  • Save jamiejackson/f404c445280fa45229a24bb32571fbc3 to your computer and use it in GitHub Desktop.

Select an option

Save jamiejackson/f404c445280fa45229a24bb32571fbc3 to your computer and use it in GitHub Desktop.
TestBox BDD Examples, Including Dynamically-Generated Tests
component
extends="testbox.system.BaseSpec"
hint="This is an example of a TestBox BDD test bundle. For (much) more information, see the documentation at http://testbox.ortusbooks.com/content/primers/bdd/index.html."
{
/******************** "Global" setup/teardown *****************************/
/*
* Need to do config for *dynamic* test suites here, in the pseudo-
* constructor, versus in beforeAll(). See explanation here:
* https://goo.gl/gosdBK
*/
doDynamicSuiteConfig();
/*
* @hint This function is arbitrarily named, but it sets up a var needed by the dynamic tests example
*/
function doDynamicSuiteConfig(){
variables.dynamicSuiteConfig = ["foo","bar","baz"];
}
/**
* @hint Executes before the *set* of all tests.
*/
function beforeAll(){}
/**
* @hint Executes after the *set* of all tests.
*/
function afterAll(){}
/*========================================================================*/
function run( testResults, testBox ){
/*
* @hint: Executes before each and every test.
*/
beforeEach(function( currentSpec ) {
// do something
});
/*
* @hint: Executes after each and every test.
*/
afterEach(function( currentSpec ) {
// do something
});
/*
* @hint Simple "describe" example
* TestBox Docs: http://testbox.ortusbooks.com/content/primers/bdd/suites_describe_your_tests.html
*/
describe("Example suite", function(){
it( "throws an error", function() {
expect( function(){
foo = bar;
}).toThrow();
});
it("has a passing sample spec", function(){
expect( true ).toBeTrue();
});
});
/*
* @hint Dynamic Test Suite Example
*/
// loop over test metadata
for ( var thing in dynamicSuiteConfig ) {
describe("Dynamic Suite #thing#", function(){
// notice how data is passed into the it() closure:
// * data={keyA=valueA, keyB=ValueB}
// * function( data )
it( title=thing & "test", data={thing=thing}, body=function( data ) {
var thing = data.thing;
expect(thing).toBe(thing);
});
});
}
/*********** Given/When/Then Syntax ***********************************/
/*
* @hint syntax for "feature"
* TestBox Docs: http://testbox.ortusbooks.com/content/primers/bdd/suite_groups.html
*
* Gherkin requirement (https://github.com/cucumber/cucumber/wiki/Gherkin):
*
* Feature: Box Size
* In order to know what size box I need
* As a distribution manager
* I want to know the volume of the box
*
* Scenario: Get box volume
* Given I have entered a width of 20
* And a height of 30
* And a depth of 40
* When I run the calculation
* Then the result should be 24000
*
*/
feature( "Box Size", function() {
describe("In order to know what size box I need
As a distribution manager
I want to know the volume of the box", function() {
scenario( "Get box volume", function(){
given( "I have a width of 20
And a height of 30
And a depth of 40",
function() {
when("I run the calculation", function() {
then("the result should be 24000", function() {
// test the outcome
expect( 20 * 30 * 40).toBe(24000); // normally, this would be a call out to some object, not an inline calculation
});
});
}
)
});
});
});
/*
* @hint syntax for user stories
* TestBox Docs: http://testbox.ortusbooks.com/content/primers/bdd/suite_groups.html
* User Stories: https://en.wikipedia.org/wiki/User_story
*/
story("As a distribution manager, I want to know the volume of the box I need", function() {
given(
"I have a width of 20
And a height of 30
And a depth of 40",
function() {
when("I run the calculation", function() {
then("the result should be 24000", function() {
expect( 20 * 30 * 40).toBe(24000); // normally, this would be a call out to some object, not an inline calculation
});
});
}
);
});
/*====================================================================*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment