Date post: | 11-May-2015 |
Category: |
Technology |
Upload: | dror-helper |
View: | 1,611 times |
Download: | 0 times |
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); }
not a
real
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”
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)
FakeDataAccess
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
12
Usage statistics (2012)
Moq45%
Rhino Mocks23%
None9%
FakeItEasy6%
Nsubstitute6%
Isolator4%
Moles2%
MS Fakes2%
JustMocks2%
Other 1%
http://osherove.com/blog/2012/5/4/annual-poll-which-isolation-framework-do-you-use-if-any.html
13
• Married into your (test) code
• Saves/waste development time
• Affects design
Choosing the right framework is crucial
Round 1 - API
• Readable
• Discoverable
• Simple
• Extensive
• Error messages
Mock-Demo
DB
Read Data
Request
Response
API summary
Error msg Parameters Builder SPE Readability
+ Explicit Yes No + Moq
- Explicit No No + Rhino-Mocks
+ Explicit No Yes + FakeItEasy
+ Explicit No No + nSubtitute
? Explicit Yes No - NMock3
+ Implicit No Yes + Isolator
- Explicit No Yes + JustMock
N/A Explicit Yes No - MS Fakes
Error msg Parameters Builder SPE Readability
+ Explicit Yes No + Moq
- Explicit No No + Rhino-Mocks
+ Explicit No Yes + FakeItEasy
+ Explicit No No + nSubtitute
? Explicit Yes No - NMock3
+ Implicit No Yes + Isolator
- Explicit No Yes + JustMock
N/A Explicit Yes No - MS Fakes
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
Round 3
ConstrainedUnconstrained
Constrained
• Fake by inheritance• Force architecture
– Dependency injection (DI)– Code by interfaces (LSP)
• Wrapping of the unfakeable
Real object
Fake object
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
21
Constraint•NMock3•FakeItEasy•nSubtitute•Moq•RhinoMocks
Unconstraint
•Isolator•JustMock•MS Fakes
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?
Round 4 - deployment
Deployment – the bottom lineMS Fakes JustMock Isolator Constraint
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
Nothing Build Server
Only TFS (> 2012) Environment varsJustMockRunnerBuild Tasks
Environment varsTMockRunnerBuild Tasks
Just run it Run tests on build machine
Only VS profilers Use Linker Automatic Linker Works! Profilers Support
25
Which framework is better?