Testing with Mock Objects
-
Upload
ben-carey -
Category
Technology
-
view
1.892 -
download
2
description
Transcript of Testing with Mock Objects
![Page 1: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/1.jpg)
testing with mock objects
![Page 2: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/2.jpg)
when i say testing…
== developer / unit tests
== integration tests (sometimes)
!= functional / acceptance tests
![Page 3: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/3.jpg)
when i say mock…
test stub
mock object
dummy object
fake object
![Page 4: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/4.jpg)
mock vs. stub interaction vs. state
the difference between mocks and stubs
usually the same library provides both
interaction = mock
state = stub
![Page 5: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/5.jpg)
why?
![Page 6: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/6.jpg)
testing is easy in isolation
Class Under Test
Test Class
![Page 7: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/7.jpg)
testing is harder with dependencies …
Class Under Test
Test Class
![Page 8: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/8.jpg)
… so remove the dependencies (for developer testing)
Class Under Test
Test Class
mock
mock
mockm
ock
![Page 9: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/9.jpg)
for me…
lots of time in the airport
lots of time in the airplane
lots of time on the train
not enough family time
not enough snowboarding
![Page 10: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/10.jpg)
scenarios to think about
fee per transaction
distributed development
parallel development
plug-ins / providers
automated test duration
![Page 11: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/11.jpg)
scenarios to think about
third-party tools or services
non-deterministic outcomes
external systems
the mainframe crew
defer infrastructure choices
![Page 12: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/12.jpg)
demo
![Page 13: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/13.jpg)
thoughts
![Page 14: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/14.jpg)
… on architecture and design
similar to the benefits of tdd
writing to interfaces = code empathy
easier to test = good design
highly cohesive / loosely coupled
![Page 15: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/15.jpg)
… on implementation
good libraries exist
roll your own only for extreme cases
for .Net…
NMock, NMock2, Rhino Mocks, DotNetMock
![Page 16: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/16.jpg)
… on automated builds
developer tests: use mocks (if necessary)
acceptance tests: don’t use mocks
![Page 17: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/17.jpg)
… on a few useful patterns
code to interfaces
thin proxies
self-shunt
dependency injection
providers
![Page 18: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/18.jpg)
learn more
![Page 19: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/19.jpg)
http://www.martinfowler.com/articles/mocksArentStubs.html
![Page 20: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/20.jpg)
http://xunitpatterns.com/
![Page 23: Testing with Mock Objects](https://reader033.fdocuments.in/reader033/viewer/2022060109/555839b0d8b42acb078b4ab9/html5/thumbnails/23.jpg)
http://www.codeproject.com/dotnet/tdd_in_dotnet.asp