2. A âunitâ is a method or function
A unit test is a piece of a code (usually a
method) that invokes another piece of code and
checks the correctnessâŚ
The Basics
4. A unit test should have the following
properties:
1. It should be automated and repeatable.
2. It should be easy to implement.
3. Once itâs written, it should remain for future
use.
4. Anyone should be able to run it.
5. It should run at the push of a button.
6. It should run quickly.
22. Using stubs to break dependencies
⢠⢠Defining stubs
⢠⢠Refactoring code to use stubs
⢠⢠Overcoming encapsulation problems in code
⢠⢠Exploring best practices when using stubs
23. DEFINITION
⢠An external dependency is an object
in your system that your code under
test interacts with, and over which
you have no control. (Common
examples are filesystems, threads,
memory, time, and so on.)
24. DEFINITION
⢠A stub is a controllable replacement for an
existing dependency (or collaborator) in the
system. By using a stub, you can test your
code without dealing with the dependency
directly.
27. The problem - an integration test
Direct Dependency test-inhibiting design
the code has some
dependency on an external
resource, which might break
the test even though the
codeâs logic is perfectly valid
28. astronaut analogy
A space shuttle simulator
⢠Find the interface or API
⢠Replace the underlying implementation
29. Introducing a layer of indirection to
avoid a direct dependency on the
filesystem
31. Refactoring our design to be more
testable
⢠DEFINITION Refactoring is the act of changing
the codeâs design without breaking existing
functionality.
⢠DEFINITION Seams are places in your code
where you can plug in different functionality,
such as stub classes.
32. techniques for breaking dependencies
⢠Extract an interface to allow replacing
underlying implementation.
⢠Inject stub implementation into a class under
test.
⢠Receive an interface at the constructor level.
⢠Receive an interface as a property get or set.
⢠Get a stub just before a method call.
38. Inject stub implementation into a
class under test
⢠Constructor
⢠Property
⢠a parameter to the method injection.
⢠a factory class
⢠a local factory method
44. Solution 1:
⢠parameter object
refactoring
create a special class that
contains all the values
needed to initialize a class
45. Solution 2:
inversion of control
(IoC) containers
⢠Spring.NET
⢠Castle Windsor
⢠Microsoft Unity
⢠Autofac
⢠Ninject
⢠StructureMap
http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx
46. Another Problem:
⢠50 tests against your
constructor
⢠another dependency
you had not considered
⢠????????
⢠change the call in 50
other tests !!!!!!!!!!!!
59. DEFINITION
Interaction testing is testing how an object
sends input to or receives input from other
objectsâhow that object interacts with other
objects.
60. DEFINITION
⢠A mock object is a fake object in the system
that decides whether the unit test has
passed or failed. It does so by verifying
whether the object under test interacted
as expected with the fake object.
⢠Thereâs usually no more than one mock per
test.
61. Example â tree watering
⢠â State-based testing
⢠â Interaction testing
76. Isolation (mock object) frameworks
⢠⢠Understanding isolation frameworks
⢠⢠Defining fake objects
⢠⢠Using Rhino Mocks to create stubs and mocks
⢠⢠Surveying advanced use cases for mocks and
stubs
⢠⢠Exploring arrange-act-assert and record-and-
replay syntax
⢠⢠Avoiding common misuses of isolation
frameworks
77. DEFINITION
⢠An isolation framework is a set of
programmable APIs that make creating mock
and stub objects much easier.
⢠Isolation frameworks save the developer from
the need to write repetitive code to test or
simulate object interactions.
80. Dynamically creating a fake object
DEFINITION
A is any stub or mock thatâs
created at runtime without needing to use a
handwritten implementation of an interface or
subclass.
98. Parameter constraints for mocks and
stubs
LogAnalyzer sends "[Some GUID] Error messageâ
an example:
"33DFCC9D-D6C5-45ea-A520-A6018C88E490 Out of memory"