Session from NDC London 2013
Writing unit tests is hard, isn't it? you need an entire set of tools just to start. One of the crucial decisions when building this set is picking up a mocking framework.
But BEWARE! what you choose has the ability to make you or break you!
Come to the session and see Dror Helper, once a mocking framework developer, show the good and the bad of different mocking frameworks (free and commercial) and make them battle to the death!
2. • Consultant @CodeValue
• Developing software (professionally) since 2002
• Disclaimer: I used to work @Typemock
• Blogger: http://blog.drorhelper.com
About.ME
3. This is unit testa
[Test]
public void AddTest()
{
var cut = new Calculator();
var result = cut.Add(2, 3);
Assert.AreEqual(5, result);
}
4. Real code has dependencies
Unit test
Code under test
Dependency Dependency
5. The solution - Mocking
Unit test
Code under test
DependencyFake object(s)
6. • Replace production logic with custom logic
• We do this in order to
– Focus the test on one class only
– Test Interaction
– Simplify Unit test writing
Isolation
7. What is a “Mock”?
“mock objects are simulated objects
that mimic the behavior of real
objects in controlled ways”
[From Wikipedia]
I prefer to call them “Fakes”
8. Manual/Hand-rolled mock
• Use inheritance to replace production logic.
• Use configurable delegates as custom logic.
PasswordChecker
PasswordChecker(IDataAccess)
CheckPassword(string, string)
DataAccess
GetUserByName(string)
SaveUser(string, string)
9. The problem
• Maintainability – it’s a lot of work
• Gets complicated as more functionality is required:
• Production logic inside fakes
• Error prone - how do we avoid bugs in test?
10. • Create Fake objects
• Set behavior on fake objects
• Verify method was called
• And more...
What Mocking framework can do for you?
11. Meanwhile in the .NET world
Open source
• FakeItEasy
• Moq
• NMock3
• nSubtitute
• Rhino Mocks
Free
• MS Fakes
Commercial
• Isolator
• JustMock
17. Round 2 - Robustness
Code under test API changes
• Method signature
• Method name
Code under test internal changes
• Unexpected call to fake
• Return value from unspecified method
Future proof
19. Constrained
• Fake by inheritance
• Force architecture
– Dependency injection (DI)
– Code by interfaces (LSP)
• Wrapping of the unfakeable
Real object
Fake object
20. Unconstrained
• Profiler API based
• Can fake almost anything
– 3rd party systems
– Legacy code
• Design your code - not for testability
• Can be used as if they are constrained
Real Object
Dependency Fake
22. Nickels and dimes
Constraint frameworks are free – but can you afford
them?
How much does it cost if every task takes 1 hour
more?
How much does it cost not to use unit tests?
24. Deployment – the bottom line
MS FakesJustMockIsolatorConstraint
Visual studio
2012/2013
Install on each
machine.
Install on each
machine.
Auto-run correct
version
Nothing
(NuGet)
Dev
machine
Only TFS (> 2012)Install on each
machine.
Install or use
AutoDeploy
NothingBuild
Server
Only TFS (> 2012)Environment vars
JustMockRunner
Build Tasks
Environment vars
TMockRunner
Build Tasks
Just run itRun
tests on
build
machine
Only VS profilersUse LinkerAutomatic LinkerWorks!Profilers
Support
Maintainability – it’s a lot of work
Gets complicated as more functionality is required:
Setting different behaviors
Multiple parameters
Checking for multiple calls
Production logic inside fakes
What happens when our code changes?
Error prone - how do we avoid bugs in test?
Possible changes:
CUT adds/remove a parameter
Dependency API change
Add call to the same dependency without specifying in test
Change to another overload of the same function