Permalink
Browse files

Added some BDD tests - not all of them are working yet

  • Loading branch information...
1 parent fc36271 commit e355c88eeaf7a68981965ca7fb8223e307e475e7 @tudorconstantin committed Mar 26, 2012
Showing with 104 additions and 4 deletions.
  1. +8 −0 .gitignore
  2. +3 −1 Makefile.PL
  3. +3 −3 lib/Boilerplate.pm
  4. +28 −0 t/behaviors.t
  5. +21 −0 t/features/mojo.feature
  6. +41 −0 t/features/step_definitions/mojo_steps.pl
View
@@ -0,0 +1,8 @@
+blib/*
+cover_db/*
+Boilerplate/*
+log/*
+Makefile
+MYMETA.json
+MYMETA.yml
+pm_to_blib
View
@@ -47,7 +47,9 @@ WriteMakefile(
'Mojolicious::Plugin::YamlConfig' => '0.1.3',
'YAML' => 0,
'YAML::XS' => 0,
- 'File::Find' => 0,
+ 'File::Find' => 0,
+ 'Test::BDD::Cucumber' => 0,
+ 'FindBin::libs' => 0,
},
test => {TESTS => 't/*.t t/*/*.t'}
);
View
@@ -28,10 +28,10 @@ sub startup {
my $r = $self->routes;
# Normal route to controller
- $r->route('/')->to('example#welcome');
+ $r->route('/')->name('home_page')->to('example#welcome');
$r->route('/home')->to('example#welcome');
- $r->route('/login')->to('example#login');
+ $r->route('/login')->name('login')->to('example#login');
$r->route('/logout')->to('example#logout');
$r->route('/sign-up')->to('example#about');
@@ -40,7 +40,7 @@ sub startup {
$r->route('/:user_type/menu')->to('example#signed_in_menu');
#route to photo gallery (controller Gallery, action gallery)
- $r->route('/clujpm_gallery/:view_as')->to('gallery#gallery');
+ $r->route('/clujpm_gallery/:view_as')->name('photo_gallery')->to('gallery#gallery');
}
View
@@ -0,0 +1,28 @@
+#!perl
+
+ use strict;
+ use warnings;
+ use Test::More;
+ use FindBin::libs;
+
+ # This will find step definitions and feature files in the directory you point
+ # it at below
+ use Test::BDD::Cucumber::Loader;
+
+ # This harness prints out nice TAP
+ use Test::BDD::Cucumber::Harness::TestBuilder;
+
+ # Load a directory with Cucumber files in it. It will recursively execute any
+ # file matching .*_steps.pl as a Step file, and .*\.feature as a feature file.
+ # The features are returned in @features, and the executor is created with the
+ # step definitions loaded.
+ my ( $executor, @features ) = Test::BDD::Cucumber::Loader->load(
+ 't/features/' );
+
+ # Create a Harness to execute against. TestBuilder harness prints TAP
+ my $harness = Test::BDD::Cucumber::Harness::TestBuilder->new({});
+
+ # For each feature found, execute it, using the Harness to print results
+ $executor->execute( $_, $harness ) for @features;
+
+ done_testing();
View
@@ -0,0 +1,21 @@
+Feature: Simple Mojolicious Application Testing
+ As a (future) prolific web developer
+ I want to test my web application easily
+ In order to be even lazier than I currently am
+
+ Background:
+ Given a mojo test object for the "Boilerplate" application
+
+ Scenario: Start as logged out
+ When I go to "home"
+ Then I should see the "Log In" url
+ And I should see the "You are not logged in" text
+ And I should see the "Mojolicious\\s+and\\s+Bootstrap" text
+
+ Scenario: Being able to login
+ When I go to "login"
+ Then I should see the "logout" url
+ And I should see the "Logged\\s+in\\s+as" text
+ And I should see the "Mojolicious\\s+and\\s+Bootstrap" text
+
+
@@ -0,0 +1,41 @@
+#!perl
+
+ use strict;
+ use warnings;
+
+ use Test::More;
+ use Test::Mojo;
+ use Test::BDD::Cucumber::StepFile;
+ use Method::Signatures;
+
+ Given qr/a mojo test object for the "(.+)" application/, func ($c) {
+ use_ok( $1 );
+ my $tm = Test::Mojo->new( $1 );
+ ok( $tm, "Object created" );
+ $c->stash->{'feature'}->{'tm'} = $tm;
+ ok( $c->stash->{'feature'}->{'tm'}, "Got our Test::Mojo object" );
+ };
+
+ When qr/I go to "(.+)"/, func ($c) {
+ my $route_url = '';
+ foreach my $route ( @{ $c->stash->{feature}->{tm}->app->routes()->children } ) {
+ $route_url = $route->pattern->pattern and last if $route->name eq $1;
+ }
+ $c->stash->{'feature'}->{'tm'}->get_ok($route_url);
+ };
+
+ Then qr/I should see the "(.+)"\s+(.+)$/, func ($c) {
+ if ( $2 eq 'url' ){
+ use_ok('Mojo::DOM');
+ my $dom = Mojo::DOM->new($c->stash->{'feature'}->{'tm'}->tx->res->body());
+ foreach my $link ( $dom->find('a[href]')->each ) {
+ pass("Found URL with text $1") and return 1 if $link->text eq $1;
+ }
+ fail( "I have not seen $1 of type $2" );
+ } elsif( $2 eq 'text'){
+ ok( $c->stash->{'feature'}->{'tm'}->content_like(qr/$1/) );
+ } else {
+ pending( "I don't know how to find $2" );
+ }
+
+ }

0 comments on commit e355c88

Please sign in to comment.