story_d 0.9.0-beta.1 copy "story_d: ^0.9.0-beta.1" to clipboard
story_d: ^0.9.0-beta.1 copied to clipboard

A BDD testing framework for Dart, implemented as a fluent DSL.

example/main.dart

import 'package:story_d/api.dart';
import 'package:test/test.dart';

/// This is a quick and dirty demonstration of how to use the StoryD fluent API
/// to create stories with scenarios and callbacks.
/// Given Dart does not support function overloading directly, tuples are used
/// help emulate function overloading, allowing for a more natural and fluent
/// syntax to be used when writing BDD styled tests.
///
/// Rename this file to anything with _test.dart (e.g.: examples_test.dart), and
/// execute using your IDE's test runner, or dart test at the command line.

void aFerritt() {}
void aCat(String param) {
  print('Kitty Cat makes a $param meow!');
}

var anonymousMethod = () {};

void iThinkIBrokeIt() {
  expect(false, true, reason: 'you broke the setup code!');
}

void main() {
  // Delibrately failing tests show how the scenario fails gracefully
  // without executing remaining scenario steps.
  story('A broken story')
      .asA('role')
      .iWant('a feature')
      .soThat('I benefit')
      .withScenario('Scenario A')
      .given(iThinkIBrokeIt.step)
      .when('this should not run'.step)
      .and('this should not run'.step)
      .then('this should not run'.step)
      .withScenario('Scenario B')
      .given('this is fine'.step)
      .when(
        "Oops!! didn't handle the exception".withCallback(() {
          throw Exception('Remember to use try..catch!!');
        }),
      )
      .then('this should not run'.step)
      .and('this should not run'.step)
      .withScenario('Scenario C')
      .given('this is fine'.step)
      .when('this is also fine'.step)
      .then('this is OK'.step)
      .and('this is okayer'.step)
      .and(iThinkIBrokeIt.step)
      .and('this should not run'.step)
      .and('this should not run'.step)
      .execute();

  // This deliberately failing test shows how the first scenario fails
  // because no callbacks have been used
  story('A test scenario checks for evidence of testing')
      .withScenario('A scenario without callbacks')
      .given('GNDN'.step)
      .when('GNDN'.step)
      .then('GNDN'.step)
      .and('GNDN'.step)
      .withScenario('A scenario with callbacks')
      .given("Does something".withCallback(() => {}))
      .execute();

  // The following tests show how stories can be set up and
  // both configured and invoked at later times

  var withFerritStory = Story("Sumthin bout a ferrit");

  withFerritStory
      .asA("Ferret")
      .iWant("to crawl up trousers")
      .soThat("i freak people out")
      .withScenario("A ferret crawling up trousers")
      .given(aFerritt.step)
      .and("a pair of trousers".step)
      .when("the ferret is released".step)
      .and("I underestimated the ferret's climbing abilities".step)
      .then("the ferret should crawl up the trousers".step)
      .and("I should be surprised".step)
      .withScenario("An otter has caught a fish")
      .given("an otter".withCallback(() {}))
      .and("a fish".step)
      .when("the otter has caught the fish".step)
      .and("the otter is hungry".step)
      .then("the otter should eat the fish".step)
      .and(
        "do a happy otter dance".withCallback(() {
          expect(true, true);
        }),
      );

  var s2 = story("Did you see a ferrit")
      .inOrderTo("get a good laugh")
      .asA("observer")
      .iCan("watch a ferrit crawl up trousers")
      .withScenario("Observer sees a trouser climbing ferrit")
      .given(
        "A ferrit already climbed the trousers"
            .withCallback((int x) => {})
            .withParams(5),
      )
      .then("the observer should be laughing".step)
      .asStory;

  // with an example showing callbacks and exception testing
  var s3 = story("Sumthin bout a cat")
      .withScenario("Cat")
      .given(aCat.withParams("loud"))
      .when(
        "the kitty glitches"
            .withCallback(() {
              throw Exception('message');
            })
            .expectException<Exception>('message'),
      )
      .asStory;

  withFerritStory.execute();
  s2.execute();
  s3.execute();
}
0
likes
160
points
97
downloads

Documentation

API reference

Publisher

verified publisherperridak.software

Weekly Downloads

A BDD testing framework for Dart, implemented as a fluent DSL.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

meta, path, perridak_strings, test

More

Packages that depend on story_d