From Sela Developer Practice 2013
In the real world, applications are so complex, that in order to test a component in isolation, you need a new set of tools. Without using a mocking framework it is impossible to write easy to read and maintain unit tests. The problem is that there are a lot of different frameworks to choose from – each has its merits and shortcomings. How can you choose the correct framework? What is the difference between the free alternatives to the costly commercial options and why should you care about error message… By the end of this talk you'll understand why you need a mocking framework and which framework is the best for your project.
2. About.Me
• Developing software (professionally) since 2002
• Disclaimer: I used to work at Typemock
• Blogger: http://blog.drorhelper.com
3. This is a unit test
[Test]
public void AddTest()
{
var cut = new Calculator();
var result = cut.Add(2, 3);
Assert.AreEqual(5, result);
}
This is not a real unit test
4. The problem - dependencies
Unit test
Code under test
Dependency Dependency
5. The solution – Mocking!
Unit test
Code under test
Dependency
Fake object(s)
6. What is a “Mock object”?
“mock objects are simulated objects that mimic
the behavior of real objects in controlled ways”
[From Wikipedia]
I prefer to call them “Fakes”
7. What does a mocking framework do?
1. Create fake objects
2. Set behavior on fake objects
3. Verify method was called (Mock)
4. Much more
8. .NET Mocking frameworks
Isolation Frameworks
Open Source
Commercial
Isolator
JustMock
RhinoMocks
Moq
nSubtitute
FakeItEasy
NMock3
Free
MS Fakes
16. Constrained
• Fake by inheritance
• Force architecture
• Dependency injection (DI)
• Code by interfaces (LSP)
• Boxing of the unfakeable
Real object
Fake object
17. 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
18. Nickels and dimes
Constraint frameworks are free – 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?
20. Deployment – the bottom line
MS FakesJustMockIsolatorUnconstraint
Only on VS11Install on each
machine.
Install on each
machine.
Auto-run correct
version
Nothing
(NuGet)
Dev
machine
Only on TFS2012Install on each
machine.
Install or use
AutoDeploy
NothingBuild
Machine
Only TFS2012Environment vars
JustMockRunner
Build Tasks
Environment vars
TMockRunner
Build Tasks
Just run itCI
Only VS11
profilers
Limited linker
(nCover, dotTrace)
Use Linker
VS11 profilers
Works!Profilers
Support
Possible changes:CUT adds/remove a parameterDependency API changeAdd call to the same dependency without specifying in testChange to another overload of the same function