Mock what? What Mock?Learn What is Mocking, and how to use Mocking with ColdFusion testing, development, and continuous integration. Look at Mocking and Stubbing with a touch of Theory and a lot of Examples, including what you could test, and what you should test… and what you shouldn't test (but might be fun).
CBDW2014 - MockBox, get ready to mock your socks off!
1. Just
Mock
It
Mocks
and
Stubs
Presented by Gavin Pickin
Slides: http://gpickin.com/itb2014/
Website: http://gpickin.com
Twitter: @gpickin
2. Introduc)on
• Follow
along
with
resources
from
h6p://gpickin.com/itb2014/
• If
you
want
to
find
out
more
about
me,
my
website
has
more
than
enough
informa)on
3. 20
Second
Agenda
• Unit
Tes)ng
-‐
Recap
• What
is
Mocking
• What
is
a
Mock
/
Stub
• Why
Mock
• Let’s
look
at
some
Mocking
4. Unit
Tes)ng
“unit
tes)ng
is
a
soOware
verifica)on
and
valida)on
method
in
which
a
programmer
tests
if
individual
units
of
source
code
are
fit
for
use.
A
unit
is
the
smallest
testable
part
of
an
applica)on”
-‐
wikipedia
5. Unit
Tes)ng
• Can
improve
code
quality
-‐>
quick
error
discovery
• Code
confidence
via
immediate
verifica)on
• Can
expose
high
coupling
• Will
encourage
refactoring
to
produce
>
testable
code
• Remember:
Tes)ng
is
all
about
behavior
and
expecta)ons
7. Bugs
Hurt
• Bugs
hurt
–
the
later
in
the
process,
the
harder
to
fix.
• Test
Early
and
OOen
– Find
them
before
they
rot
your
founda)on
• Testable
Code
is
Maintainable
Code
10. Important
Tests
• Unit
Tes)ng
– Test
behavior
of
individual
objects
• Integra)on
Tes)ng
– Test
En)re
Applica)on
from
Top
Down
• UI
verifica)on
tes)ng
– Verifica)on
via
HTML/Visual
elements
11. Important
Tes)ng
Tools
• TestBox
(Run
BDD
and
MXUnit
style)
• IDE
-‐
CF
Builder
/
Eclipse
• Mocking
Framework
• ANT
• Jenkins,
Bamboo,
Teamcity,
other
Cis
• Selenium
• Jmeter
or
Webstress
Tool,
Apache
AB
12. What
is
Mocking?
According
to
Merriam
Webster
• to
laugh
at
or
make
fun
of
(someone
or
something)
especially
by
copying
an
ac)on
or
a
way
of
behaving
or
speaking
• to
imitate
(as
a
mannerism)
closely
13. What
is
Mocking?
"A
mock
object
is
an
object
that
takes
the
place
of
a
‘real’
object
in
such
a
way
that
makes
tes)ng
easier
and
more
meaningful,
or
in
some
cases,
possible
at
all"
by
Sco6
Bain
-‐
Emergent
Design
18. Stub
Object
• A
stub
is
an
empty
container,
that
represents
an
Object.
• This
can
be
useful
for
represen)ng
CFCs
and
Objects
that
haven’t
been
wri6en
yet.
19. Why
use
Mocking
• Isolate
your
SUT
-‐>
SoOware
Under
Test
• To
build
against
interfaces
&
contracts
• Building
against
missing
integra)on
pieces
• To
control
data
and
expecta)ons
• Mock
components
whose
behavior
is
undesirable
or
hard
to
control
20. Why
Mock?
• How
do
you
test
when
helper
components
that
are
not
built
yet?
• How
do
you
do
controlled
excep)ons?
• How
do
you
test
&
control
external
API
calls?
• How
do
you
control
results
from
ColdFusion
tags
or
func)ons?
• How
do
you
control
network
connec)ons?
Do
you
pull
the
network
plug?
21. Why
Mock?
How
do
you
test
code
like
this?
<cfdirectory
ac)on=”list”
directory=”#arguments.path#”
name=”qResults”>
<ck6p
url=”#arguments.urlPath#”
results=”qResults”>
<cfmail
to=”#to#”
from=”#from#”
subject=”#subject#”>#content#</cfmail>
<cfquery
/>
func)on
init(){
var
helper
=
new
Helper();
}
private
func)on
getData(){
return
data;
}
22. The
COLD
hard
truth
–
It
can
Hurt
• Tes)ng
some
code
is
hard,
or
almost
impossible.
• Refactor
it
• Mock
it
• Test
It
• With
CI,
you
can
forget
about
it
Un)l
it
breaks
23. Refactor
to
make
it
Mockable
• Original:
<cfdirectory
ac)on=”list”
directory=”/myapp/path”
name=”qResults”>
• Refactored
<cffunc)on
name=”getFiles”
output=”false”
returnType=”query”>
<cfargument
name=”path”>
<cfset
var
qResults
=
“”>
<cfdirectory
ac)on=”list”
directory=”#arguments.path#”
name=”qResults”>
...
Process
Here
...
<cfreturn
qResults>
</cffunc)on>
28. What
can
MockBox
do?
• Mock
Objects
with
or
without
implementa)ons
• Mock
methods
&
proper)es
in
any
scope
• Create
Stub
Objects
-‐>
Non-‐existent
objects
• Mock
excep)ons
• Mock
arguments
to
results
• Logging
&
Debugging
• Verifica)on
methods
• State
Machine
Results
29. Sepng
up
MockBox
• Standalone
Version
(FW/1,
Fusebox,
Model
Glue,
Home
Brewed)
mockBox
=
createObject(“component”,”mockBox.system.tes)ng.MockBox”).init();
• Running
from
inside
ColdBox
(outside
of
TestBox)
mockBox
=
createObject(“component”,”coldbox.system.tes)ng.MockBox”).init();
30. Using
MockBox
• MockBox
does
its
magic
dynamically,
adding
/
decora)ng
CFCs
with
necessary
methods.
• CreateMock()
user
=
mockBox.createMock(“model.User”);
• CreateEmptyMock()
dao
=
mockBox.createEmptyMock(“model.UserDAO”);
• PrepareMock()
mockBox.prepareMock(
service
);
31. Using
MockBox
-‐
Stubs
• CreateStub()
– Create
a
simple
empty
Stub
– Pass
an
Implements
to
get
a
Mock
Interface
– Pass
an
Extends
to
get
a
Mock
with
Inheritance
nonExistentService
=
mockBox.createStub();
mockInterface
=
mockBox.createStub(implements=”model.ICache”);
mockInheritance
=
mockbox.createStub(extends=”model.SecurityService”);
35. Using
$args()
• Results
based
on
the
Arguments
Passed
• Must
be
chained
via
Results.
//
Call
to
Mock
if(
dao.getSepng(“userAudit”)
){
startAudit(
dao.getSepng(“auditTables”)
);
};
//
Mocking
Calls
dao.$(“getSepng”).$args(“userAudit”).$results(true);
dao.$(“getSepng”).$args(“auditTables”).$results(“user,order,
36. Using
$args()
• Arguments
are
smart
like
normal
func)ons
– Posi)onal
Arguments
saveUser(”Rose”,”Tyler”);
– Named
Arguments
saveUser(fname=”Rose”,lname=”Tyler”);
– Argument
Collec)ons
data
=
{
fname
=
“Rose”,
lname
=
“Tyler”
};
saveUser(argumentCollec)on=data);
37. $results()
• Your
results
can
be
– Constant
– Or
Sequenced
• Your
results
will
loop
over
and
over.
• Argument
Based
Results
can
also
be
repe))ve
in
a
sequence.
38. $property()
• Mock
any
property
on
any
scope
• Great
for
sepngs
and
dependency
injec)on
mocking
//
Mock
a
sepng
on
the
variables
scope
service.$property(“cacheAc)ve”,”variables”,true);
//
Mock
a
file
u)lity
object
mockU)l
=
mockbox.createEmptyMock(“u)l.FileU)ls”);
service.$property(“fileU)l”,”variables”,
mockU)l);
//
Mock
in
the
variables.instance
scope
path
service.$property(“isDirty”,”instance”,true);
42. Lets
look
at
a
Demo
h6p://justmockit.local.com
Code
available
at:
h6ps://github.com/gpickin/justmockitdemo
43. The
End
–
Thank
you
• Thanks
everyone
for
making
it
this
far
• All
of
the
materials
will
be
on
my
ITB
presenta)on
Site:
h6p://www.gpickin.com/itb2014/
• Hit
me
up
on
twi6er
@gpickin
• Read
more
on
my
blog:
h6p://gpickin.com
• Check
out
my
CFO
Session
Thursday,
15th
11:30am
–
12:30am
on
Lakes
Ballroom
C