2. Here’s
how
to
get
started
(it’s
surprisingly
easy)
• I’m
assuming
you’ve
got
Rails
3/RVM/all
that
jazz
working.
• If
you
don’t,
I
can’t
help
you…
…and,
someIme
when
you
least
expect
it,
MaK
will
hurt
you.
3. Here’s
how
to
get
started
(it’s
surprisingly
easy)
• Add
to
Gemfile:
group :test do
gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'rspec-rails'
gem 'spork'
gem 'launchy'
gem 'machinist', '>= 2.0.0.beta2'
gem 'faker'
end
• Bundle
install
4. Here’s
how
to
get
started
(it’s
surprisingly
easy)
• Now,
to
set
up
your
rails
app.
bundle exec rails g cucumber:install --capybara
bundle exec rails g machinist:install
touch features/support/blueprints.rb
• Add
this
line
to
blueprints.rb:
require 'machinist/active_record’
• Edit
this
line
in
env.rb
(in
features/support)
Cucumber::Rails::World.use_transactional_fixtures = false
5. Why
we’re
using
Machinist
• I
wanted
to
start
from
with
a
blank
slate,
installing
Cucumber
and
nothing
else.
• However,
I
quickly
found
that
Machinist
makes
things
a
lot
easier.
• Machinist
gives
you
blueprints,
greatly
simplifying
the
creaIon
of
mock
objects.
• More
later…
6. WriIng
features
@classifieds
Feature: Classified Ads
In order to buy and sell things
As a logged in user
I want to be able to view, add and manage classified ads
Background:
Given I am logged into cas as "Peter" "Portal" with an
username of "00700001"
And I have created another user
And I am on the classifieds index page
Scenario: Should be able to see the ads
Then I should see "Glamlife Marketplace"
Scenario: Should be able to add an ad
When I follow "Create a new ad"
And I fill in "classified_title" with "Testing title"
And I select "For Sale" from "classified_ad_type"
7. Baby
steps
• Each
of
those
lines,
aUer
the
feature
definiIon,
is
called
a
‘step’.
• There
are
a
bunch
of
simple
steps
that
Cucumber
knows
all
about,
but
someImes
you
will
want
to
do
something
more
complex
than
“click
this
link”.
• A
good
example
is
logging
in
using
CAS:
Given I am logged into cas as "Peter" "Portal" with an
username of "00700001"
• How
does
cucumber
know
what
to
do
with
this?
8. Defining
steps
• So
that
Cucumber
understands
our
steps,
we
have
to
define
them:
Given /^I am logged into cas as "(.*)" "(.*)" with an username of
"(.*)"$/ do | firstname, surname, username |
@user = User.make!(
:username => username,
:firstname => firstname,
:surname => surname,
:dob => "01/01/1990",
:home_address1 => "1 Street Street",
:home_email => "pedro@portal.po",
:home_phone => "1234567890",
:term_address1 => "5 Glam Street",
:term_phone => "0987654321",
:term_mobile => "5674839201",
:user_status => "Student",
:user_type => "UGStudent"
)
CASClient::Frameworks::Rails::Filter.fake(username)
visit("/cas_session?force_cas_user=#{@user.username}")
end
9. CAS
hates
green
salad
fruits
• CAS
and
Cucumber
don’t
get
on
at
all
well.
• Luckily
the
rubycas-‐client
gem
developer
has
put
in
a
way
for
Cucumber
to
get
around
CAS
without
taking
a
beaIng.
CASClient::Frameworks::Rails::Filter.fake(username)
• Adding
this
line
to
our
step
definiIon
lets
Cucumber
in
the
back
door.
• Green
peppers,
however,
can
get
bent.
10. Blueprints
and
‘make!’
• Blueprints
allow
you
to
pre-‐define
instances
of
your
models,
so
you
don’t
have
to
fill
in
all
the
details.
• Using
make!
instead
of
create
uses
these
blueprints.
• Let’s
have
a
look
at
a
blueprint.rb
file…
11. Blueprints
and
‘make’
Chunk.blueprint do
title { sentence }
content { sentence }
end
Classified.blueprint do
title { sentence }
ad_type { ad_type }
body { paragraphs }
reason { sentence }
image { File.new("#{Rails.root.to_s}/
features/support/upload-files/image.png") }
moderated { 0 }
completed { 0 }
end
12. Faking
data
• The
variables
in
the
last
slide
are
set
up
to
create
random
data,
oUen
using
Faker.
sentence
=
Faker::Lorem.sentence
paragraphs
=
Faker::Lorem.paragraphs.join("nn")
ad_type
=
"For
Sale"
name
=
Faker::Name.name
first_name
=
Faker::Name.first_name
last_name
=
Faker::Name.last_name
username
=
rand(99999999).to_s
13. Paths
• In
olden
Imes,
people
used
to
have
to
put
paths
to
pages
into
their
features.
• This
is
‘briKle’
because
if
a
path
changes,
your
test
will
break.
To
miIgate
this
problem,
paths
are
defined
in
paths.rb
in
features/support,
eg:
when /the homes?page/
'/'
• This
way,
you
only
have
to
update
one
file
when
something
changes.
• The
same
can
be
done
with
selectors…
14. Selectors
• Selectors
are
used
to
‘select’
a
specific
instance
of
something
on
a
page.
• For
example,
you
might
have
the
same
text
appear
in
more
than
one
place
and
want
to
restrict
it
to
checking
a
specific
div.
• For
example:
Then I should see "Ad was successfully created"
within ".flash.notice”
• This
checks
for
the
text
within
a
div
with
the
classes
“flash”
and
“noIce”.
15. Selectors
• But
this
doesn’t
look
very
nice,
and
it
is
briKle.
• So,
we
can
introduce
a
selectors.rb
file,
similar
to
paths.rb,
to
provide
natural
language
shortcuts:
when /the flash (notice|error|info)/
".flash.#{$1} "
• Now
we
can
say:
Then I should see "Ad was successfully created"
within the flash notice
• See
this
site
for
how
to
set
this
up:
http://bjeanes.com/2010/09/19/selector-free-cucumber-
scenarios
16. Running
the
tests
• Once
you
have
wriKen
your
features,
simply
run:
bundle exec cucumber
• to
run
a
single
feature,
use
tags:
Or,
bundle exec cucumber –t tagname
• You
should
see
loads
of
red
–
now
write
your
code
and
make
it
green!