Download - Triumph Over Legacy Code with Unit Testing
![Page 1: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/1.jpg)
Testing Legacy CodeExciting approach to unit testing any code,Keeping high quality & agility, and Creating wicked applications
Eli Lopian, Founder Typemock
![Page 2: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/2.jpg)
WHAT YOU WILL
LEARN
• Problems with legacy code • How isolation helps • How to unit test legacy code (demo)
![Page 3: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/3.jpg)
Legacy code is… Code without testsMichael Feathers
![Page 4: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/4.jpg)
Developers write millions of lines of legacy code
every day!
![Page 5: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/5.jpg)
Legacy Code Problems
• Code is tangled• Complex Tests• Death spiral• Catch-22
![Page 6: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/6.jpg)
Isolate it… (The most effective way to test legacy code )
• Fake Dependencies
• Change Code Behavior
• Verify Calls
![Page 7: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/7.jpg)
Fake Dependencies
var fakeProcess = Isolate.Fake.Instance<Process>();fakeProcess.MainModule.Site.Name
var fakeProcessHandle = Isolate.Fake.NextInstance<Process>(); var fakeAllProcessHandle = Isolate.Fake.AllInstances<Process>();
var fakeSimulator = Isolate.Fake.Dependencies<Simulator>();
Create a fake instance of a type:1
Signal that the next instance will be faked:2
Fake ALL the dependencies that are passed through the constructor:3
![Page 8: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/8.jpg)
Change Code Behavior
Isolate.WhenCalled(() => DateTime.Now).WillReturn(new DateTime(1977,5,25));
Isolate.WhenCalled(() => fake.MainModule.Site.Name).WillReturn("Typemock rocks");
var counter = new Counter ();Isolate.WhenCalled(() => counter.Increment()).WillReturn(1);Isolate.WhenCalled(() => counter.Increment()).WillReturn(2);
Change return value of a static method:1
Change return value of a chain of calls:
Change return value of a method from a real object:
2
3
![Page 9: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/9.jpg)
Verify Calls
Isolate.Verify.WasCalledWithAnyArguments(() => backoffice.Save());
Isolate.Verify.WasNotCalled(() => backoffice.Save());
int count = Isolate.Verify.GetTimesCalled(() => backoffice.Save());Assert.AreEqual(2, count);
The code called Save():
The code never calls Save():
The code calls the method 2 times:
1
2
3
![Page 10: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/10.jpg)
![Page 11: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/11.jpg)
Cannot call constructor because of dependencies
![Page 12: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/12.jpg)
Need method to return fake value
![Page 13: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/13.jpg)
Nothing to assert
![Page 14: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/14.jpg)
Fake an object to change its behavior.
![Page 15: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/15.jpg)
Need to change behavior of a private method
![Page 16: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/16.jpg)
Need to Fake an object that is created in the code
![Page 17: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/17.jpg)
• Isolate Dependencies
• Control Behavior
• Assert Calls
Fake It Till You Make It!
![Page 18: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/18.jpg)
PowerfulMocking
Visual Coverage SmartRunner
Typemock Isolator
Productivity*
* Complete Edition Only
![Page 19: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/19.jpg)
• Trial License• Online webinars• Cookbook• Team Demonstration
You can get more resources
![Page 20: Triumph Over Legacy Code with Unit Testing](https://reader036.vdocument.in/reader036/viewer/2022062404/554d22bbb4c905c5208b4c05/html5/thumbnails/20.jpg)
www.typemock.com
blog.typemock.com
@typemock
Questions?
Image creditsflickr@33909700@N02flickr@jamesukflickr@nikioflickr@duchampflickr@kalyan02flickr@clarkstonscampflicker@95786359@N05